-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzpgui.m
More file actions
351 lines (304 loc) · 11.5 KB
/
Copy pathzpgui.m
File metadata and controls
351 lines (304 loc) · 11.5 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
function varargout = zpgui(varargin)
% ZPGUI Zero Pole dragging Graphic User Interface
% Allows you to add, remove, and move the zeros and
% poles of a filter interactively.
% To begin, type:
% zpgui
% Author: Tom Krauss, 9/1/98
% Adapted by David Dorran (2011) to force poles and zeros to be conjugate
% pairs (maybe not the most flexible feature but it saves me time).
% Updated in 2012 to plot the z-surface.
% J.W. Peltenburg, 18 dec 2014
% Fixed some warnings and errors concerning older version of MATLAB, this
% script is now compatible with r2014b.
% Added impulse response to the lower right corner
% Changed 3d mesh to 3d surface plot
% Removed pushbutton to remove 3d view area fill
% Other minor cosmetic changes were made
% J.W. Peltenburg, 14 december 2015
% Changed "normalised" frequency from [0...1] to [0...2pi]
global zh ph Lresp Nfft b a z h Y
global ax1 ax2 ax3 ax4 z_surface_CameraPos z_surface_CameraUpVec surface_display_opts
if nargin == 0
action = 'init';
z = [0 -1]' ;
p = [1/2+1/2*j 1/2-1/2*j]' ;
elseif nargin >= 3
p = varargin{1}';
z = varargin{2}';
action = 'init';
else
action = varargin{1};
end
if nargin == 3
jpg_filename = varargin{3};
end
switch action
case 'init'
z_surface_CameraPos=[3.7719 -15.7111 275.3541];
z_surface_CameraUpVec=[-0.1237 0.5153 23.1286];
surface_display_opts = 0;
set(0,'defaultaxesfontsize',10)
subplot(2,2,1)
[zh,ph,cruff]=zplaneplot(z,p);
%set(cruff,'hittest','off')
ax1 = gca;
ylabel('Imaginary Part')
xlabel('Real Part')
uicontrol('style','pushbutton',...
'string','Add Zeros',...
'fontsize',8,...
'units','normalized',...
'position',[.15 .46 .18 .04],...
'callback','zpgui(''addzero'')');
uicontrol('style','pushbutton',...
'string','Remove Zeros',...
'fontsize',8,...
'units','normalized',...
'position',[.55 .46 .18 .04],...
'callback','zpgui(''removezero'')');
uicontrol('style','pushbutton',...
'units','normalized',...
'fontsize',8,...
'position',[.35 .46 .18 .04],'string','Add Poles',...
'callback','zpgui(''addpole'')');
uicontrol('style','pushbutton',...
'units','normalized',...
'fontsize',8,...
'position',[.75 .46 .18 .04],'string','Remove Poles',...
'callback','zpgui(''removepole'')');
%uicontrol('style','pushbutton',...
% 'units','normalized',...
% 'fontsize',8,...
% 'position',[.5 .96 .18 .04],'string','Toggle surface display',...
% 'callback','zpgui(''toggle_surface_display'')');
subplot(2,2,3)
[b,a]=zp2tf(z,p,1);
Nfft = 512;
Y = fft(b,Nfft)./fft(a,Nfft);
Lresp = plot((0:Nfft-1)/Nfft*2-1, 20*log10(fftshift(abs(Y))),'linewidth',2,'color',[0 0 0]);
%Lresp = plot((0:Nfft-1)/Nfft*2-1, fftshift(abs(Y)),'linewidth',2);
ax2 = gca;
set(ax2,'xlim',[0 1])
%set(ax2,'ylim',[max(min(Lresp),0.1) max(Lresp)])
set(ax2,'xtick', [0:1/4:1])
set(ax2,'xticklabel', {'0','\pi/4','\pi/2','2\pi/3','\pi'})
%get(ax2,'xticklabel')
grid on
xlabel('Frequency (\Omega)')
ylabel('Magnitude (dB) ')
%set(Lresp,'erasemode','xor')
set(zh,'buttondownfcn','zpgui(''zeroclick'')',...
'markersize',8,'linewidth',1)
set(ph,'buttondownfcn','zpgui(''poleclick'')',...
'markersize',8,'linewidth',1)
subplot(2,2,2)
ax3 = gca;
plot_z_surface(ax3, p,z);
set(gcf, 'ToolBar', 'figure');
if(nargin == 3) % create some figures ans save them to a file
figure
set(gcf,'position', [ 232 387 417 279]);
plot_z_surface(p,z);
saveas(gcf, strcat('z_surface_', jpg_filename, '.bmp'), 'bmp');
figure
set(gcf,'position', [ 232 387 417 279]);
plot((0:Nfft-1)/Nfft - .5, 20*log10(fftshift(abs(Y))),'linewidth',2);
set(gca,'xlim',[0 .5])
grid on
xlabel('Normalised Frequency')
ylabel('Magnitude (db) ')
saveas(gcf, strcat('z_response_', jpg_filename, '.bmp'), 'bmp');
figure
set(gcf,'position', [ 232 387 417 279]);
zplaneplot(z,p)
ylabel('Imaginary Part')
xlabel('Real Part')
saveas(gcf, strcat('z_pz_', jpg_filename, '.bmp'), 'bmp');
figure
set(gcf,'position', [232 605 392 61])
b_coeff_vals = poly(z)
a_coeff_vals = poly(p)
text(0.05,0.1 ,strcat('a = [',num2str(a_coeff_vals) ,']'))
text(0.05,0.8 ,strcat('b = [',num2str(b_coeff_vals) ,']'))
set(gca, 'visible','off')
pause(0.1)
saveas(gcf, strcat('z_ba_', jpg_filename, '.bmp'),'bmp');
close all
end
subplot(2,2,4);
if (size(b,2) <= size(a,2))
[y,t]=impulse(tf(b,a,1));
stem(t,y,'filled','black');
xlabel('Sample');
ylabel('\delta response h[n]');
else
stem([0 0]);
xlabel('Cannot compute impulse response');
ylabel('\delta response h[n]');
end
case 'toggle_surface_display'
surface_display_opts = surface_display_opts + 1;
if(surface_display_opts >= 2)
surface_display_opts = 0;
end
zpgui('recompute')
case 'addzero'
if length(zh)>0
zh(end+1) = line(.5,0,'parent',ax1,'buttondownfcn','zpgui(''zeroclick'')',...
'markersize',8,'linewidth',1,'marker','o','linestyle','none');
else
zh = line(.5,0,'parent',ax1,'buttondownfcn','zpgui(''zeroclick'')',...
'markersize',8,'linewidth',1,'marker','o','linestyle','none');
set(findobj('string','Remove Zeros'),'enable','on')
end
zpgui('recompute')
case 'removezero'
delete(zh(end))
zh(end)=[];
if length(zh)==0
set(gco,'enable','off')
end
zpgui('recompute')
case 'addpole'
if length(ph)>0
ph(end+1) = line(.5,0,'parent',ax1,'buttondownfcn','zpgui(''poleclick'')',...
'markersize',8,'linewidth',1,'marker','x','linestyle','none');
else
ph = line(.5,0,'parent',ax1,'buttondownfcn','zpgui(''poleclick'')',...
'markersize',8,'linewidth',1,'marker','x','linestyle','none');
set(findobj('string','Remove Poles'),'enable','on')
end
zpgui('recompute')
case 'removepole'
delete(ph(end))
ph(end)=[];
if length(ph)==0
set(gco,'enable','off')
end
zpgui('recompute')
case 'zeroclick'
set(gcf,'userdata','')
set(gcf,'windowbuttonmotionfcn','set(gcf,''userdata'',''motion'')')
set(gcf,'windowbuttonupfcn','set(gcf,''userdata'',''up'')')
ind = find(zh==gco);
%set(zh(ind),'erasemode','xor')
% set(Lresp,'erasemode','xor')
pair = length(get(zh(ind),'xdata'))==2;
done = 0;
pt = get(ax1,'currentpoint');
pt = pt(1,1:2);
title(['selected position: ' num2str(pt) 'j'])
while ~done
waitfor(gcf,'userdata')
switch get(gcf,'userdata')
case 'motion'
pt = get(ax1,'currentpoint');
pt = pt(1,1:2);
title(['selected position: ' num2str(pt) 'j'])
%if pair
if 1
set(zh(ind),'xdata',[pt(1) pt(1)],'ydata',[pt(2) -pt(2)])
else
set(zh(ind),'xdata',pt(1),'ydata',pt(2))
end
zpgui('recompute')
case 'up'
done = 1;
end
set(gcf,'userdata','')
end
set(gcf,'windowbuttonmotionfcn','')
set(gcf,'windowbuttonupfcn','')
%set(zh(ind),'erasemode','normal')
%set(Lresp,'erasemode','normal')
set(ax2,'ylimmode','auto')
ylim = get(ax2,'ylim');
Y = get(Lresp,'ydata');
if ylim(1)>min(Y)-3 | ylim(2)<max(Y)+3
set(ax2,'ylim',[min(Y)-3 max(Y)+3])
end
case 'poleclick'
set(gcf,'userdata','')
set(gcf,'windowbuttonmotionfcn','set(gcf,''userdata'',''motion'')')
set(gcf,'windowbuttonupfcn','set(gcf,''userdata'',''up'')')
ind = find(ph==gco);
% set(ph(ind),'erasemode','xor')
%set(Lresp,'erasemode','xor')
pair = length(get(ph(ind),'xdata'))==2;
done = 0;
pt = get(ax1,'currentpoint');
pt = pt(1,1:2);
title(['selected position: ' num2str(pt) 'j'])
while ~done
waitfor(gcf,'userdata')
switch get(gcf,'userdata')
case 'motion'
pt = get(ax1,'currentpoint');
pt = pt(1,1:2);
title(['selected position: ' num2str(pt) 'j'])
if 1
set(ph(ind),'xdata',[pt(1) pt(1)],'ydata',[pt(2) -pt(2)])
else
set(ph(ind),'xdata',pt(1),'ydata',pt(2))
end
zpgui('recompute')
case 'up'
done = 1;
end
set(gcf,'userdata','')
end
set(gcf,'windowbuttonmotionfcn','')
set(gcf,'windowbuttonupfcn','')
%set(ph(ind),'erasemode','normal')
%set(Lresp,'erasemode','normal')
set(ax2,'ylimmode','auto')
Y = get(Lresp,'ydata');
ylim = get(ax2,'ylim');
if ylim(1)>min(Y)-3 | ylim(2)<max(Y)+3
set(ax2,'ylim',[min(Y)-3 max(Y)+3])
end
case 'recompute'
z = [];
p = [];
b = 1;
a = 1;
for i=1:length(zh)
zx = get(zh(i),'xdata');
zy = get(zh(i),'ydata');
if length(zx)==1
b = conv(b,[1 -(zx+sqrt(-1)*zy)]);
else
b = conv(b,[1 -2*zx(1) zx(1).^2+zy(1).^2]);
end
z = [z zx+sqrt(-1)*zy];
end
for i=1:length(ph)
px = get(ph(i),'xdata');
py = get(ph(i),'ydata');
if length(px)==1
a = conv(a,[1 -(px+sqrt(-1)*py)]);
else
a = conv(a,[1 -2*px(1) px(1).^2+py(1).^2]);
end
p = [p px+sqrt(-1)*py];
end
Y = fft(b,Nfft)./fft(a,Nfft);
%Y = Y/max(abs(Y));
set(Lresp,'ydata',20*log10(fftshift(abs(Y))))
subplot(2,2,2)
z_surface_CameraPos = campos();%get(ax3, 'CameraPosition');
z_surface_CameraUpVec = camup();%;get(ax3, 'CameraUpVector');
plot_z_surface(ax3, p, z, z_surface_CameraPos, z_surface_CameraUpVec, surface_display_opts);
subplot(2,2,4);
if (size(b,2) <= size(a,2))
[y,t]=impulse(tf(b,a,1));
stem(t,y,'filled','black');
xlabel('Sample');
ylabel('\delta response h[n]');
else
stem([0 0]);
xlabel('Cannot compute impulse response');
ylabel('h[n]');
end
end