-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwvfrm_generator_streamer.m
115 lines (102 loc) · 3.94 KB
/
wvfrm_generator_streamer.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
clear all
close all
% ------------------ settings ------------------ %<<<1
% clock frequency (frequency of samples of the BPG):
fclock = 13.76256e9;
% number of periods in every amplitude section:
P = 10;
% points in memory sections (number of points per frequency section):
% (sum should be <= memory of the BPG):
% (lcm(4, 10, 128) = 640, where 4 amplitudes, 10 periods per amp. section, 128 is word in BPG)
M = 640 .* 11200 .* [16 8 4 2];
% amplitudes of the signal:
A = [0.1, 0.3, 0.5, 0.7];
% bit multiplication:
B = 32;
% ADC sampling frequency:
adc.fs = 480e3;
% simulated noise (v rms):
simnoise = 1e-10;
% simulated drift (volts/second):
simdrift = 0;
% ------------------ preparation ------------------ %<<<1
% number of amplitude sections in frequency section:
K = length(A);
% number of frequency/memory sections:
L = length(M);
% signal frequencies in every frequency section:
f = fclock ./ M .* (P .* K);
% variable containing waveform:
y = [];
% variable containing ADC samples:
yadc = [];
% ------------------ waveform construction ------------------ %<<<1
% for every frequency/memory section:
for i = 1:L
% counter vector for one period of sine wave in current amplitude section:
% (no bit multiplication here)
cnt = [1 : M(i) ./ (P .* K)];
% for every amplitude:
for curA = A
% BPG samples %<<<2
% generate samples for one period of signal in current amplitude section:
tmpy = curA .* sin(2 .* pi .* cnt .* f(i) ./ fclock);
% repeat to get required number of periods in current amplitude section:
y = [y repmat(tmpy, 1, P)];
% ADC samples %<<<2
% time of ADC samples for whole current amplitude section:
tadc = [0 : 1./adc.fs : P ./ f(i) .* B ];
tadc = tadc(1:end-1);
tmpyadc = curA .* sin(2 .* pi .* tadc .* f(i) ./ B);
% concatenate waveform into metawaveform:
yadc = [yadc tmpyadc];
end % K amplitudes
end % L frequencies
% ------------------ saving the ADC data ------------------ %<<<1
adc.offset = mean(yadc);
tmp = yadc - adc.offset;
adc.gain = (max(yadc) + 10.*simnoise)/double(intmax('int32'));
fn = 'simulated_data.bin';
fid = fopen(fn, 'w');
% number of metawaveform periods:
mwperiods = 4000; % cca 3.6G file
mwperiods = 40;
t = 0;
for i = 1:mwperiods
% add noise to simulation:
yadc = yadc + normrnd(0, simnoise, size(yadc));
% add drift to simulation:
yadc = yadc + (t.*simdrift + cumsum(ones(size(adc)).*simdrift./adc.fs));
t = t + length(yadc)./adc.fs;
yadcint = yadc - adc.offset;
yadcint = yadcint./adc.gain;
% convert to int32:
yadcint = int32(yadcint);
fwrite(fid, yadcint, 'int32', 0, 'ieee-le');
endfor
fclose(fid);
% info file:
infostr = [];
infostr = [infostr infosettext('data format', '32-bit signed integer, little endian')];
infostr = [infostr infosetnumber('data points', sum(y).*mwperiods)];
infostr = [infostr infosetnumber('sample rate', adc.fs)];
infostr = [infostr infosetnumber('data gain', adc.gain)];
infostr = [infostr infosetnumber('data offset', adc.offset)];
infostr = infosetsection('measurement settings', infostr);
infosave(infostr, fn, 'info', true);
% ------------------ plotting BPG samples (one metawaveform) ------------------ %<<<1
% decimate data by factor of X for faster plotting:
ydec = y(1:1000:end);
ysdec = y(1:1000:end);
plot(ydec, '-', ysdec, 'x')
%%% print -djpg wv-time.jpg
% % ------------------ fft of BPG samples ------------------ %<<<1
%
% disp('calculating 50x spectra')
% ylarge = repmat(y, 1, 50);
% [F, AMP, PH] = ampphspectrum(y, 1, 0);
% disp('finished')
% plot(F(1:1e4), AMP(1:1e4))
% print -djpg wv-spectrum.jpg
%
% vim modeline: vim: foldmarker=%<<<,%>>> fdm=marker fen ft=octave textwidth=1000