-
Notifications
You must be signed in to change notification settings - Fork 19
/
signa.m
63 lines (55 loc) · 1.61 KB
/
signa.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function signa(waveform,filename,scale)
%
% signa(waveform, filename [,scale]);
%
% writes the waveform out as short integers with the low
% bit masked off.
%
% Inputs:
% waveform -- vector, may be complex
% filename -- string, if wavefrom is complex '.r' and '.i' are appended,
% and two files are written.
% scale -- optional scale. If unspecified, the waveform is scaled to
% full scale integer 32766. If specified, the output is
% waveform*scale*32766
%
%
% Written by John Pauly, Dec. 5, 1994
% (c) Leland Stanford Jr. University
%
wmax = hex2dec('7ffe');
% if no scale is specified, use as much dynamic range as possible
if nargin == 2,
scale = 1/max(max(abs(real(waveform)),abs(imag(waveform))));
end;
% scale up to fit in a short integer
waveform = waveform*scale*wmax;
% mask off low bit, since it would be an EOS otherwise
waveform = 2*round(waveform/2);
% if the imaginary component is zero, supress it
if sum(abs(imag(waveform))) == 0,
waveform = real(waveform);
end;
if isreal(waveform),
fip = fopen(filename,'wb','b');
if fip == -1,
disp(sprintf('Error opening %s for write',filename));
return;
end;
fwrite(fip,waveform,'short');
else
fip = fopen([filename,'.r'],'wb','b');
if fip == -1,
disp(sprintf('Error opening %s for write',[filename,'.r']));
return;
end;
fwrite(fip,real(waveform),'short');
fclose(fip);
fip = fopen([filename,'.i'],'wb','b');
if fip == -1,
disp(sprintf('Error opening %s for write',[filename,'.i']));
return;
end;
fwrite(fip,imag(waveform),'short');
fclose(fip);
end;