📄 filtdemo.m
字号:
helpHndl closeHndl methodHndl];
set(figNumber, ...
'Visible','on', ...
'UserData',fhndlList);
set(gcf,'Pointer','watch');
drawnow
axes(freqzHnd)
set(freqzHnd,'Userdata',[])
filtdemo('design')
set(gcf,'Pointer','arrow','handlevisibility','callback');
set(0,'ShowHiddenHandles',shh)
return
elseif strcmp(action,'changemethod'),
v = get(gco,'value'); % 1 = REMEZ, 2 = FIRLS, 3 = KAISER,
% 4 = BUTTER, 5 = CHEBY1, 6 = CHEBY2, 7 = ELLIP
fhndlList = get(gcf,'UserData');
if v == 2
set(fhndlList(8),'enable','off')
set(fhndlList(8),'value',0,'userdata',2);
set(fhndlList(9),'value',1)
else
set(fhndlList(8),'enable','on')
set(fhndlList(8),'value',1,'userdata',1);
set(fhndlList(9),'value',0)
end
filtdemo('design')
return
elseif strcmp(action,'setFreqs'),
v = get(gco,'Userdata');
str = get(gco,'String');
ind = find(abs(str)<32);
str(ind) = 32*ones(size(ind));
str = str';
vv = eval(['[' str(:)' ']'],'-1')';
if vv == -1
vv = v;
elseif length(vv)~=3
vv = v;
elseif any(vv<0) | vv(2)>=vv(3) | vv(2)>=vv(1)/2 | ...
vv(3)>=vv(1)/2
vv = v;
end
vv = round(vv*10)/10;
str = sprintf('%g\n\n%g\n\n%g',vv(1),vv(2),vv(3));
set(gco,'Userdata',vv,'String',str)
if any(vv~=v)
filtdemo('design')
else
filtdemo('axesredraw')
end
return
elseif strcmp(action,'setRipples'),
v = get(gco,'Userdata');
str = get(gco,'String');
ind = find(abs(str)<32);
str(ind) = 32*ones(size(ind));
str = str';
vv = eval(['[' str(:)' ']'],'-1')';
if vv == -1
vv = v;
elseif any(vv<=0) | length(vv)~=2
vv = v;
end
str = sprintf('%g\n\n%g',vv(1),vv(2));
set(gco,'Userdata',vv,'String',str)
if any(vv~=v)
filtdemo('design')
else
filtdemo('axesredraw')
end
return
elseif strcmp(action,'dragRp'),
% s = 1 ==> down, s = 2 ==> mouse motion, s = 3 ==> mouse up
if s == 1 % down
fhndlList=get(gcf,'Userdata');
freqs = get(fhndlList(4),'UserData');
Wp = freqs(2); % passband frequency
ripples = get(fhndlList(5),'UserData');
Rp = ripples(1); % passband ripple
method = get(fhndlList(14),'value');
hndlList=get(gca,'Userdata');
the_line = hndlList(2);
set(the_line,'erasemode','xor')
pt = get(gca,'currentpoint');
pt_x = pt(1,1); pt_y = pt(1,2);
if (method > 3)&(pt_y > -Rp/2)
return % don't drag the 0 dB line in IIR case
end
% how many pixels wide is the Wp line?
set(gca,'units','pixels');
pos = get(gca,'position');
set(gca,'units','normalized');
xl = get(gca,'xlim');
Wp_in_pixels = Wp*pos(3)/diff(xl);
% use right-most 5 pixels, or one fifth of them, which ever is smaller
width = min(round(Wp_in_pixels/5),5);
if pt_x < Wp-width/pos(3)*diff(xl)
set(gcf,'windowbuttonmotionfcn','filtdemo(''dragRp'',2)')
set(gcf,'windowbuttonupfcn','filtdemo(''dragRp'',3)')
else
set(gcf,'windowbuttonmotionfcn','filtdemo(''dragWp'',2)')
set(gcf,'windowbuttonupfcn','filtdemo(''dragWp'',3)')
end
elseif s == 2 % motion
fhndlList=get(gcf,'Userdata');
method = get(fhndlList(14),'value');
hndlList=get(gca,'Userdata');
the_line = hndlList(2);
pt = get(gca,'currentpoint');
pt_y = pt(1,2);
if method<=3 % FIR case
if pt_y > 6.02, pt_y = 6.02; end
if pt_y >= 0
dev = 10^(pt_y/20)-1;
else
dev = 1-10^(pt_y/20);
end
above = 20*log10(1+dev);
below = 20*log10(1-dev);
else % IIR case
if pt_y > 0, pt_y = 0; end
above = 0; below = pt_y;
end
set(the_line,'Ydata',[above above NaN below below]);
drawnow
elseif s == 3 % up
fhndlList=get(gcf,'Userdata');
method = get(fhndlList(14),'value');
pt = get(gca,'currentpoint');
pt_y = pt(1,2);
if method<=3 % FIR case
if pt_y > 6.02, pt_y = 6.02; end
if pt_y >= 0
dev = 10^(pt_y/20)-1;
else
dev = 1-10^(pt_y/20);
end
above = 20*log10(1+dev);
below = 20*log10(1-dev);
else % IIR case
if pt_y > 0, pt_y = 0; end
above = 0; below = pt_y;
end
Rp = above - below;
set(gcf,'windowbuttonmotionfcn','')
set(gcf,'windowbuttonupfcn','')
old_ripples = get(fhndlList(5),'UserData');
rips = [Rp; old_ripples(2)];
str = sprintf('%g\n\n%g',rips(1),rips(2));
set(fhndlList(5),'Userdata',rips,'String',str)
if Rp ~= old_ripples(1)
filtdemo('design')
else
filtdemo('axesredraw')
end
end
return
elseif strcmp(action,'dragRs'),
% s = 1 ==> down, s = 2 ==> mouse motion, s = 3 ==> mouse up
if s == 1 % down
fhndlList=get(gcf,'Userdata');
freqs = get(fhndlList(4),'UserData');
Wp = freqs(2); % passband frequency
Ws = freqs(3);
hndlList=get(gca,'Userdata');
the_line = hndlList(3);
set(the_line,'erasemode','xor')
pt = get(gca,'currentpoint');
pt_x = pt(1,1);
% how many pixels wide is the Ws line?
set(gca,'units','pixels');
pos = get(gca,'position');
set(gca,'units','normalized');
xl = get(gca,'xlim');
Ws_in_pixels = Ws*pos(3)/diff(xl);
% use left-most 5 pixels, or one fifth of them, which ever is smaller
width = min(round(Ws_in_pixels/5),5);
if pt_x > Ws+width/pos(3)*diff(xl)
set(gcf,'windowbuttonmotionfcn','filtdemo(''dragRs'',2)')
set(gcf,'windowbuttonupfcn','filtdemo(''dragRs'',3)')
else
set(gcf,'windowbuttonmotionfcn','filtdemo(''dragWs'',2)')
set(gcf,'windowbuttonupfcn','filtdemo(''dragWs'',3)')
end
elseif s == 2 % motion
hndlList=get(gca,'Userdata');
the_line = hndlList(3);
pt = get(gca,'currentpoint');
set(the_line,'Ydata',[pt(1,2) pt(1,2)])
drawnow
elseif s == 3 % up
pt = get(gca,'currentpoint');
Rs = max(0,-pt(1,2));
set(gcf,'windowbuttonmotionfcn','')
set(gcf,'windowbuttonupfcn','')
fhndlList=get(gcf,'Userdata');
old_ripples = get(fhndlList(5),'UserData');
rips = [old_ripples(1); Rs];
str = sprintf('%g\n\n%g',rips(1),rips(2));
set(fhndlList(5),'Userdata',rips,'String',str)
if Rs ~= old_ripples(2)
filtdemo('design')
else
filtdemo('axesredraw')
end
end
return
elseif strcmp(action,'dragWp'),
% s = 1 ==> down, s = 2 ==> mouse motion, s = 3 ==> mouse up
if s == 2 % motion
fhndlList=get(gcf,'Userdata');
freqs = get(fhndlList(4),'UserData');
Wp = freqs(2); % passband frequency
Ws = freqs(3); % stopband frequency
hndlList=get(gca,'Userdata');
the_line = hndlList(2);
pt = get(gca,'currentpoint');
pt_x = pt(1,1);
if pt_x > Ws, pt_x = Ws; elseif pt_x < 0, pt_x = 0; end
set(the_line,'Xdata',[0 pt_x NaN 0 pt_x]);
drawnow
elseif s == 3 % up
fhndlList=get(gcf,'Userdata');
old_freqs = get(fhndlList(4),'UserData');
old_Wp = old_freqs(2); % passband frequency
Ws = old_freqs(3); % stopband frequency
pt = get(gca,'currentpoint');
pt_x = pt(1,1);
if pt_x >= Ws, pt_x = old_Wp; end
Wp = pt_x;
Wp = round(Wp*10) / 10;
set(gcf,'windowbuttonmotionfcn','')
set(gcf,'windowbuttonupfcn','')
freqs = [old_freqs(1); Wp; old_freqs(3)];
str = sprintf('%g\n\n%g\n\n%g',freqs(1),freqs(2),freqs(3));
set(fhndlList(4),'Userdata',freqs,'String',str)
if Wp ~= old_Wp
filtdemo('design')
else
filtdemo('axesredraw')
end
end
return
elseif strcmp(action,'dragWs'),
% s = 1 ==> down, s = 2 ==> mouse motion, s = 3 ==> mouse up
if s == 2 % motion
fhndlList=get(gcf,'Userdata');
freqs = get(fhndlList(4),'UserData');
Fs = freqs(1); % sampling frequency
Wp = freqs(2); % passband frequency
Ws = freqs(3); % stopband frequency
hndlList=get(gca,'Userdata');
the_line = hndlList(3);
pt = get(gca,'currentpoint');
pt_x = pt(1,1);
if pt_x < Wp, pt_x = Wp; elseif pt_x > Fs/2, pt_x = Fs/2; end
set(the_line,'Xdata',[pt_x Fs/2]);
drawnow
elseif s == 3 % up
fhndlList=get(gcf,'Userdata');
freqs = get(fhndlList(4),'UserData');
Fs = freqs(1); % sampling frequency
Wp = freqs(2); % passband frequency
old_Ws = freqs(3); % stopband frequency
pt = get(gca,'currentpoint');
pt_x = pt(1,1);
if pt_x < Wp | pt_x > Fs/2
pt_x = old_Ws;
end
Ws = pt_x;
Ws = round(Ws*10) / 10;
set(gcf,'windowbuttonmotionfcn','')
set(gcf,'windowbuttonupfcn','')
freqs = [freqs(1); freqs(2); Ws];
str = sprintf('%g\n\n%g\n\n%g',freqs(1),freqs(2),freqs(3));
set(fhndlList(4),'Userdata',freqs,'String',str)
if Ws ~= old_Ws
filtdemo('design')
else
filtdemo('axesredraw')
end
end
return
elseif strcmp(action,'setord'),
hndlList=get(gcf,'Userdata');
v = get(gco,'Userdata');
s = get(gco,'String');
vv = eval(s,num2str(v(1)));
if vv<3, vv = v; end
set(gco,'Userdata',vv,'String',num2str(vv))
if vv~=v
set(hndlList(8),'value',0,'userdata',2) % switch radio buttons
set(hndlList(9),'value',1)
filtdemo('design')
else
if get(hndlList(8),'userdata') == 1
set(hndlList(8),'value',0,'userdata',2) % switch radio buttons
set(hndlList(9),'value',1)
filtdemo('design')
end
end
return
elseif strcmp(action,'radio'),
hndlList=get(gcf,'Userdata');
for i=8:9,
set(hndlList(i),'value',0) % Disable all the buttons
end
set(hndlList(s+7),'value',1) % Enable selected button
old_one = get(hndlList(8),'Userdata');
set(hndlList(8),'Userdata',s) % Remember selected button
if s~=old_one
filtdemo('design')
else
filtdemo('axesredraw')
end
return
elseif strcmp(action,'design'), % Design filter
set(gcf,'Pointer','watch');
% get handles
axHndl=gca;
fhndlList=get(gcf,'Userdata');
freqzHndl = fhndlList(1);
FreqsHndl = fhndlList(4);
RipplesHndl = fhndlList(5);
viewHndl = fhndlList(7);
btn1Hndl = fhndlList(8);
btn2Hndl = fhndlList(9);
ord1Hndl = fhndlList(10);
ord2Hndl = fhndlList(11);
methodHndl = fhndlList(14);
colors = get(gca,'colororder');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -