-
Notifications
You must be signed in to change notification settings - Fork 54
/
dsdemo5.m
executable file
·251 lines (235 loc) · 6.69 KB
/
dsdemo5.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
% Demonstrate the simulateESL function
echo off;
if exist('LiveDemo','var') == 0
LiveDemo=0;
end
clc;
fprintf(1,'\t\tMismatch-Shaping Unit-Element DAC\n\n');
% Specify the modulator NTF, the mismatch-shaping TF, and the number of elements
echo on;
ntf = synthesizeNTF(3,[],[],4);
M = 16;
sigma_d = 0.01; % 1% mismatch
mtf1 = zpk(1,0,1,1); %First-order shaping
echo off;
A = 1/sqrt(2); % Test tone amplitude, relative to full-scale.
f = 0.33; % Test tone frequency, relative to fB.
% (Will be adjusted to be an fft bin)
if LiveDemo
N = 2^12;
else
N = 2^14;
end
fin = round(f*N/(2*12));
w = (2*pi/N)*fin;
echo on;
u = M*A*sin(w*[0:N-1]);
v = simulateDSM(u,ntf,M+1); % M unit elements requires an M+1-level quant.
v = (v+M)/2; % scale v to [0,M]
sv0 = thermometer(v,M);
sv1 = simulateESL(v,mtf1,M);
echo off
figure(1); clf
T = 20;
subplot(211);
plotUsage(sv0(:,1:T));
set(gcf,'NumberTitle','off');
set(gcf,'Name','Element Usage');
title('Thermometer-Coding')
subplot(212);
plotUsage(sv1(:,1:T));
title('First-Order Shaping');
if LiveDemo
set(1,'position',[9 204 330 525]);
changeFig(18,.5,1);
pause
end
ideal = v;
% DAC element values
e_d = randn(M,1);
e_d = e_d - mean(e_d);
e_d = sigma_d * e_d/std(e_d);
ue = 1 + e_d;
% Convert v to analog form, assuming no shaping
dv0 = ue' * sv0;
% Convert sv to analog form
dv1 = ue' * sv1;
window = ds_hann(N);
spec = fft(ideal.*window)/(M*N/8);
spec0 = fft(dv0.*window)/(M*N/8);
spec1 = fft(dv1.*window)/(M*N/8);
figure(2); clf
set(gcf,'NumberTitle','off');
set(gcf,'Name','Spectra');
plotSpectrum(spec0,fin,'r');
hold on;
plotSpectrum(spec1,fin,'b');
plotSpectrum(spec,fin,'g');
axis([1e-3 0.5 -200 0]);
x1 = 2e-3; x2=1e-2; y0=-180; dy=dbv(x2/x1); y3=y0+3*dy;
plot([x1 x2 x2 x1],[y0 y0 y3 y0],'k')
text(x2, (y0+y3)/2,' 60 dB/decade')
hold off;
grid;
ylabel('PSD');
xlabel('Normalized Frequency');
if LiveDemo
figure(1);
set(1,'position',[9 427 200 300]);
changeFig;
subplot(211)
xlabel('')
figure(2);
set(2,'position',[237 288 553 439]);
changeFig(18,2,12);
legend('thermom.','1^{st}-Order','Ideal');
set(gcf,'NumberTitle','off');
set(gcf,'Name','Output Spectra');
pause
changeFig;
legend;
set(2,'position',[238 427 325 300]);
end
legend('thermometer','rotation','ideal DAC');
fprintf(1,'Paused.\n');
pause
%Now repeat the above for second-order shaping
echo on;
mtf2 = zpk([ 1 1 ], [ 0 0 ], 1, 1); %Second-order shaping
sv2 = simulateESL(v,mtf2,M);
echo off;
figure(1); clf
T = 20;
subplot(211);
plotUsage(sv1(:,1:T));
title('First-Order Shaping');
subplot(212);
plotUsage(sv2(:,1:T));
title('Second-Order Shaping');
if LiveDemo
set(1,'position',[9 204 330 525]);
changeFig(18,.5,1);
pause
end
dv2 = ue' * sv2;
spec2 = fft(dv2.*window)/(M*N/8);
figure(2); clf
plotSpectrum(spec1,fin,'r');
hold on;
plotSpectrum(spec2,fin,'b');
plotSpectrum(spec,fin,'g');
axis([1e-3 0.5 -200 0]);
x1 = 2e-3; x2=1e-2; y0=-180; dy=dbv(x2/x1); y3=y0+3*dy;
plot([x1 x2 x2 x1],[y0 y0 y3 y0],'k')
text(x2, (y0+y3)/2,' 60 dB/decade')
legend('1^{st}-Order','2^{nd}-Order','Ideal');
hold off;
grid;
xlabel('Normalized Frequency');
ylabel('PSD');
if LiveDemo
figure(1);
set(1,'position',[9 427 200 300]);
changeFig;
subplot(211)
xlabel('')
figure(2);
set(2,'position',[237 288 553 439]);
changeFig(18,2,12);
legend('1^{st}-Order','2^{nd}-Order','Ideal');
pause
changeFig;
legend('1^{st}-Order','2^{nd}-Order','Ideal');
set(2,'position',[238 427 325 300]);
end
if 0
% Plot everything
figure(1); clf
T = 20;
subplot(311);
plotUsage(thermometer(v(1:T),M));
set(gcf,'NumberTitle','off');
set(gcf,'Name','Element Usage');
title('Thermometer-Coding')
subplot(312);
plotUsage(sv1(:,1:T));
title('First-Order Shaping');
subplot(313);
plotUsage(sv2(:,1:T));
title('Second-Order Shaping');
%printmif(fullfile('MIF','elementUsage'), [3 6], 'Helvetica10')
figure(2); clf
plotSpectrum(spec0,fin,'r');
hold on;
plotSpectrum(spec1,fin,'r');
plotSpectrum(spec2,fin,'b');
plotSpectrum(spec,fin,'g');
axis([1e-3 0.5 -200 0]);
x1 = 2e-3; x2=1e-2; y0=-180; dy=dbv(x2/x1); y3=y0+3*dy;
plot([x1 x2 x2 x1],[y0 y0 y3 y0],'k')
text(x2, (y0+y3)/2,' 60 dB/decade')
legend('Thermometer','1^{st}-Order','2^{nd}-Order','Ideal');
hold off;
grid;
xlabel('Normalized Frequency');
ylabel('PSD');
printmif(fullfile('MIF','mmSpetcra'), [3 3], 'Helvetica10')
end
if 0 % Quadrature example
order = 4;
osr = 32;
M = 8;
f0 = 1/16;
quadrature = 1;
[f1 f2] = ds_f1f2(osr,f0,quadrature);
ntf = synthesizeQNTF(order,osr,f0,-50,-10);
Amp = undbv(-3); % Test tone amplitude, relative to full-scale.
f = 0.3; % Test tone frequency offset from f0, relative to bw.
% (Will be adjusted to be an fft bin)
N = 2^12;
f1_bin = round(f1*N);
f2_bin = round(f2*N);
fin = round(((1-f)/2*f1 + (f+1)/2*f2)*N);
u = Amp*M*exp((2i*pi/N)*fin*[0:N-1]);
v = simulateQDSM(u,ntf,M+1);
sv0 = [2i*thermometer((imag(v)+M)/2,M)-1i;
2*thermometer((real(v)+M)/2,M)-1];
mtf1 = zpk(exp(2i*pi*f0),0,1,1); %First-order complex shaping
sv1 = simulateQESL(v,mtf1,M);
figure(1); clf
subplot(121);
plotUsage(sv0(:,1:20));
subplot(122);
plotUsage(sv1(:,1:20));
%printmif(fullfile('MIF','QUsage'), [32 4], 'Helvetica8')
ue = 1 + 0.01*randn(2*M,1);
dv0 = ue' * sv0;
dv1 = ue' * sv1;
window = ds_hann(N);
Nsm = 32;
NBW = 1.5/N * Nsm/2 / 1.5; % Approximate correction for smoothing; amplitude correction is also approximate
spec = fft(v.*window)/(M*N/2);
spec0 = fft(dv0.*window)/(M*N/2);
spec1 = fft(dv1.*window)/(M*N/2);
spec_sm = circ_smooth(abs(fftshift(spec)).^2,Nsm)* Nsm/3;
spec0_sm = circ_smooth(abs(fftshift(spec0)).^2,Nsm)* Nsm/3;
spec1_sm = circ_smooth(abs(fftshift(spec1)).^2,Nsm)* Nsm/3;
freq = linspace(-0.5,0.5,N+1); freq(end)=[];
figure(2); clf
plot(freq,dbp(spec_sm),'k','Linewidth',1);
hold on;
plot(freq,dbp(spec0_sm),'r','Linewidth',1);
plot(freq,dbp(spec1_sm),'b','Linewidth',1);
snr = calculateSNR(spec(f1_bin+1:f2_bin+1),fin-f1_bin);
snr0 = calculateSNR(spec0(f1_bin+1:f2_bin+1),fin-f1_bin);
snr1 = calculateSNR(spec1(f1_bin+1:f2_bin+1),fin-f1_bin);
msg = sprintf('Ideal: SNR = %.0fdB', snr);
msg0 = sprintf('No MS: SNR = %.0fdB', snr0);
msg1 = sprintf('1^{st}-order MS: SNR = %.0fdB', snr1);
text(0.5,-95,sprintf('NBW=%.0e ',NBW),'Hor','right');
figureMagic([-0.5 0.5],1/16,4, [-100 0],10,2,[6 3],'Spectra');
xlabel('frequency')
ylabel('PSD (dBFS/NBW)');
legend(msg,msg0,msg1);
printmif(fullfile('MIF','QMS'), [4 2], 'Helvetica8')
end