📄 cztdemo.m
字号:
function cztdemo(action,s);
%CZTDEMO Demonstrates the FFT and CZT in the Signal Processing Toolbox.
% Copyright (c) 1988-98 by The MathWorks, Inc.
% $Revision: 1.17 $ $Date: 1997/12/02 18:37:01 $
% Possible actions:
% initialize
% Fs
% Wmin
% Wmax
% points
% radius1
% radius2
% design
% button callbacks:
% radio
% info
% close
if nargin<1,
action='initialize';
end;
if strcmp(action,'design'), % evaluate fft or czt
set(gcf,'Pointer','watch');
hndlList=get(gcf,'Userdata');
zplaneHndl = hndlList(1);
responseHndl = hndlList(2);
FsHndl = hndlList(3); Fs = get(FsHndl,'UserData');
WminHndl = hndlList(4); Wmin = get(WminHndl,'UserData');
WmaxHndl = hndlList(5); Wmax = get(WmaxHndl,'UserData');
pointsHndl = hndlList(6); Npoints = get(pointsHndl,'UserData');
radius1Hndl = hndlList(7); R1 = get(radius1Hndl,'UserData');
radius2Hndl = hndlList(8); R2 = get(radius2Hndl,'UserData');
btn1Hndl = hndlList(9);
btn2Hndl = hndlList(10); iir = get(btn2Hndl,'UserData');
b = iir(1,:); a = iir(2,:);
closeHndl = hndlList(12); domainHndl = get(closeHndl,'UserData');
set(gcf,'nextplot','add')
if (get(btn1Hndl,'UserData')==1), % FFT button checked?
n = (ceil(Npoints*Fs / max(Wmax-Wmin,eps)));
if n>2048, n = 2^nextpow2(n); end
w = (0:n-1)'/n*2*pi;
ww = (0:min(n,400)-1)'/min(n,400)*2*pi;
axes(zplaneHndl)
if ~isempty(domainHndl)
set(domainHndl(1),'xdata',cos(ww),'ydata',sin(ww));
set(domainHndl(2),'xdata',[0 cos(Wmin*2*pi/Fs)],...
'ydata',[0 sin(Wmin*2*pi/Fs)]);
set(domainHndl(3),'xdata',[0 cos(Wmax*2*pi/Fs)],...
'ydata',[0 sin(Wmax*2*pi/Fs)]);
else
[domainHndl,h2,h3] = zplane(exp(j*ww),[],zplaneHndl);
if length(h3)>1, delete(h3(2:length(h3))), end
rcolor = get(gcf,'defaultaxescolororder');
rcolor = rcolor(min(2,size(rcolor,1)),:);
domainHndl(2) = line('xdata',[0 cos(Wmin*2*pi/Fs)],...
'color',rcolor,'ydata',[0 sin(Wmin*2*pi/Fs)]);
domainHndl(3) = line('xdata',[0 cos(Wmax*2*pi/Fs)],...
'color',rcolor,'ydata',[0 sin(Wmax*2*pi/Fs)]);
set(closeHndl,'UserData',domainHndl)
end
title('Domain of FFT')
axes(responseHndl)
F = fft(b,n)./fft(a,n);
plot(w*Fs/2/pi,20*log10(abs(F)),'.')
if (Wmin == 0)&(Wmax>=Fs*(1-1/n))
title(sprintf('%g point FFT of elliptic bandpass filter',n));
else
title(sprintf('Close-up of %g point FFT of elliptic bandpass filter',n));
end
else % CZT button checked.
M = Npoints;
A = R1*exp(j*Wmin*2*pi/Fs);
W = ( R2/R1 )^(-1/(M-1)) * exp(-j*(Wmax-Wmin)*2*pi/Fs/(M-1)) ;
axes(zplaneHndl)
z = A*W.^(-(0:M-1)');
if ~isempty(domainHndl)
set(domainHndl,'xdata',real(z),'ydata',imag(z));
set(domainHndl(2),'xdata',[0 R1*cos(Wmin*2*pi/Fs)],...
'ydata',[0 R1*sin(Wmin*2*pi/Fs)]);
set(domainHndl(3),'xdata',[0 R2*cos(Wmax*2*pi/Fs)],...
'ydata',[0 R2*sin(Wmax*2*pi/Fs)]);
else
[domainHndl,h2,h3] = zplane(z,[],zplaneHndl);
if length(h3)>1, delete(h3(2:length(h3))), end
rcolor = get(gcf,'defaultaxescolororder');
rcolor = rcolor(min(2,size(rcolor,1)),:);
domainHndl(2) = line('xdata',[0 R1*cos(Wmin*2*pi/Fs)],...
'color',rcolor,'ydata',[0 R1*sin(Wmin*2*pi/Fs)]);
domainHndl(3) = line('xdata',[0 R2*cos(Wmax*2*pi/Fs)],...
'color',rcolor,'ydata',[0 R2*sin(Wmax*2*pi/Fs)]);
set(closeHndl,'UserData',domainHndl)
end
title('Domain of CZT')
axes(responseHndl)
w = unwrap(angle(z));
w = linspace(Wmin,Wmax,M)*2*pi/Fs;
F = czt(b,M,W,A)./czt(a,M,W,A);
cla
hold on
plot(w*Fs/2/pi,20*log10(abs(F)),'.')
hold off
title(sprintf('%g point CZT of elliptic bandpass filter',M));
end
xlabel('Frequency')
ylabel('Magnitude of Transform (dB)')
set(gca,'xlim',[Wmin Wmax])
ylim = get(gca,'ylim');
set(gca,'ylim',[max(-100,ylim(1)) ylim(2)])
set(gcf,'Pointer','arrow');
return
elseif strcmp(action,'initialize'),
shh = get(0,'showhiddenhandles');
set(0,'showhiddenhandles','on')
figNumber=figure( ...
'Name','CZT and FFT Demo', ...
'handlevisibility','callback',...
'integerhandle','off',...
'NumberTitle','off');
%[b,a]=ellip(9,3,50,[.4 .7]); % design filter - store in btn2 userdata
% inline filter coeffs for speed:
b = [ 1.036215553331465e-02
2.103525287321029e-02
4.618180706244246e-02
6.626949942636884e-02
1.047645705817928e-01
1.135461439917620e-01
1.182161372812089e-01
9.184711304310156e-02
5.839803125783760e-02
-5.115907697472721e-13
-5.839803125883236e-02
-9.184711304391158e-02
-1.182161372818484e-01
-1.135461439921812e-01
-1.047645705820628e-01
-6.626949942649851e-02
-4.618180706250907e-02
-2.103525287323005e-02
-1.036215553332198e-02]';
a = [ 1.000000000000000e+00
2.616784951166920e+00
9.010794557412463e+00
1.664491445555174e+01
3.429175523852171e+01
4.935300363227517e+01
7.526259700870287e+01
8.775364685258239e+01
1.063920847920586e+02
1.018212388254725e+02
1.008239318769620e+02
7.874570918388088e+01
6.397972011971305e+01
3.962383394162555e+01
2.603920255580061e+01
1.187892654523706e+01
6.065579909513929e+00
1.633745653505110e+00
5.883332413893858e-01]';
%==================================
% Set up the image axes
axes( ...
'Units','normalized', ...
'Position',[0.10 0.35 0.60 0.6], ...
'XTick',[],'YTick',[], ...
'Box','on');
set(figNumber,'defaultaxesposition',[0.10 0.1 0.60 0.80])
zplaneHndl = subplot(2,1,1);
set(gca, ...
'Units','normalized', ...
'XTick',[],'YTick',[], ...
'Box','on');
responseHndl = subplot(2,1,2);
set(gca, ...
'Units','normalized', ...
'XTick',[],'YTick',[], ...
'Box','on');
%====================================
% Information for all buttons (and menus)
labelColor=[0.8 0.8 0.8];
yInitPos=0.90;
menutop=0.95;
btnTop = 0.6;
top=0.75;
left=0.785;
btnWid=0.175;
btnHt=0.06;
textHeight = 0.05;
textWidth = 0.07;
% Spacing between the button and the next command's label
spacing=0.019;
%====================================
% The CONSOLE frame
frmBorder=0.019; frmBottom=0.04;
frmHeight = 0.92; frmWidth = btnWid;
yPos=frmBottom-frmBorder;
frmPos=[left-frmBorder yPos frmWidth+2*frmBorder frmHeight+2*frmBorder];
h=uicontrol( ...
'Style','frame', ...
'Units','normalized', ...
'Position',frmPos, ...
'BackgroundColor',[0.5 0.5 0.5]);
%====================================
% fft radio button
btnTop = menutop-spacing;
btnNumber=1;
yPos=btnTop-(btnNumber-1)*(btnHt+spacing);
labelStr='FFT';
callbackStr='cztdemo(''radio'',1);';
% Generic button information
btnPos=[left yPos-btnHt btnWid btnHt];
btn1Hndl=uicontrol( ...
'Style','radiobutton', ...
'Units','normalized', ...
'Position',btnPos, ...
'String',labelStr, ...
'value',0,'Userdata',2, ...
'Callback',callbackStr);
%====================================
% czt radio button
btnTop = menutop-spacing;
btnNumber=2;
yPos=btnTop-(btnNumber-1)*(btnHt+spacing);
labelStr='CZT';
callbackStr='cztdemo(''radio'',2);';
% Generic button information
btnPos=[left yPos-btnHt btnWid btnHt];
btn2Hndl=uicontrol( ...
'Style','radiobutton', ...
'Units','normalized', ...
'Position',btnPos, ...
'String',labelStr, ...
'value',1, ...
'UserData',[b;a], ... % store filter coefficients here
'Callback',callbackStr);
yPos = yPos - spacing;
%===================================
% Sampling Frequency
top = yPos - btnHt - spacing;
labelWidth = frmWidth-textWidth-.01;
labelBottom=top-textHeight;
labelLeft = left;
labelRight = left+btnWid;
labelPos = [labelLeft labelBottom labelWidth textHeight];
h = uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',labelPos, ...
'Horiz','left', ...
'String','Fs', ...
'Interruptible','off', ...
'BackgroundColor',[0.5 0.5 0.5], ...
'ForegroundColor','white');
% Text field
textPos = [labelRight-textWidth labelBottom textWidth textHeight];
callbackStr = 'cztdemo(''Fs'')';
FsHndl = uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Position',textPos, ...
'Horiz','right', ...
'Background','white', ...
'Foreground','black', ...
'String','1000','Userdata',1000, ...
'callback',callbackStr);
%===================================
% Wmin frequency (1) label and text field
labelBottom=top-2*textHeight-spacing;
labelLeft = left;
labelPos = [labelLeft labelBottom labelWidth textHeight];
h = uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',labelPos, ...
'String','Fmin', ...
'Horiz','left', ...
'Interruptible','off', ...
'Background',[0.5 0.5 0.5], ...
'Foreground','white');
% Text field
textPos = [labelRight-textWidth labelBottom textWidth textHeight];
callbackStr = 'cztdemo(''Wmin'')';
WminHndl = uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Position',textPos, ...
'Horiz','center', ...
'Background','white', ...
'Foreground','black', ...
'String','200','Userdata',200, ...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -