📄 sigdemo1.m
字号:
% u = get(gcf,'userdata');
freq=SIGDEMO1_DAT(1);
amp=SIGDEMO1_DAT(2);
N=SIGDEMO1_DAT(3);
M=SIGDEMO1_DAT(4);
min_dB=SIGDEMO1_DAT(5);
fixed_x=SIGDEMO1_DAT(6);
fixed_y=SIGDEMO1_DAT(7);
time_line=SIGDEMO1_DAT(8);
freq_line=SIGDEMO1_DAT(9);
freq_field=SIGDEMO1_DAT(10);
popup=SIGDEMO1_DAT(11);
t=SIGDEMO1_DAT(14:14+N-1);
window=SIGDEMO1_DAT(14+N:14+N+N-1);
pt=get(gca,'currentpoint');
x=pt(1,1);
y=pt(1,2);
amp1=y/fixed_y*amp;
if (abs(amp1)>1.0),
amp1=1.0*sign(amp1);
end;
if (abs(amp1)<0.05),
amp1=0.05*sign(amp1);
end;
if (amp1 == 0),
amp1=0.05;
end;
freq1=fixed_x/x*freq;
val = get(popup,'Value');
if (val == 1),
f=amp1*sin(freq1*t*2*pi);
elseif (val == 2), % square wave
tt=freq1*t*2*pi;
tmp=rem(tt,2*pi);
f=amp1*(2*rem((tt<0)+(tmp>pi | tmp<-pi)+1,2)-1);
elseif (val == 3), % sawtooth
tt=freq1*t*2*pi;
f=amp1*((tt < 0) + rem(tt,2*pi)/2/pi - .5)*2;
end;
set(time_line,'YData',f);
F=fft(window.*f,2*M);
F=F(1:M);
w=(0:M-1)*pi/M;
FF=20*log10(abs(F));
ind=find(FF<min_dB);
FF(ind)=NaN*ones(size(ind)); % put NaN's in where
% min_dB shows up - this is to work around no clipping in xor mode
set(freq_line,'YData',FF);
set(freq_field,'String',num2str(freq1));
elseif strcmp(action,'up'),
pt=get(gca,'currentpoint');
x=pt(1,1);
y=pt(1,2);
set(gcf,'WindowButtonMotionFcn','');
set(gcf,'WindowButtonUpFcn','');
% u=get(gcf,'userdata');
freq=SIGDEMO1_DAT(1);
amp=SIGDEMO1_DAT(2);
fixed_x=SIGDEMO1_DAT(6);
fixed_y=SIGDEMO1_DAT(7);
amp1=y/fixed_y*amp;
if (abs(amp1)>1.0),
amp1=1.0*sign(amp1);
end;
if (abs(amp1)<0.05),
amp1=0.05*sign(amp1);
end;
freq1=fixed_x/x*freq;
set(SIGDEMO1_DAT(8),'linestyle','-');
SIGDEMO1_DAT(1)=freq1; % set amplitude and frequency
SIGDEMO1_DAT(2)=amp1;
% set(gcf,'userdata',u);
sigdemo1('redraw');
elseif strcmp(action,'done'),
% close the figure window that is showing the window fnction:
% u = get(gcf,'userdata');
if (SIGDEMO1_DAT(12)~=-1),
close(SIGDEMO1_DAT(12));
end;
close(gcf);
clear global SIGDEMO1_DAT
clear global ADDIT_DAT
elseif strcmp(action,'redraw'),
% recomputes time and frequency waveforms and updates display
% u = get(gcf,'userdata');
freq=SIGDEMO1_DAT(1);
amp=SIGDEMO1_DAT(2);
N=SIGDEMO1_DAT(3);
M=SIGDEMO1_DAT(4);
min_dB=SIGDEMO1_DAT(5);
time_line=SIGDEMO1_DAT(8);
freq_line=SIGDEMO1_DAT(9);
freq_field=SIGDEMO1_DAT(10);
popup=SIGDEMO1_DAT(11);
t=SIGDEMO1_DAT(14:14+N-1);
window=SIGDEMO1_DAT(14+N:14+N+N-1);
val = get(popup,'Value');
if (val == 1),
f=amp*sin(freq*t*2*pi);
elseif (val == 2), % square wave
tt=freq*t*2*pi;
tmp=rem(tt,2*pi);
f=amp*(2*rem((tt<0)+(tmp>pi | tmp<-pi)+1,2)-1);
elseif (val == 3), % sawtooth
tt=freq*t*2*pi;
f=amp*((tt < 0) + rem(tt,2*pi)/2/pi - .5)*2;
end;
set(time_line,'YData',f);
F=fft(window.*f,2*M);
F=F(1:M);
w=(0:M-1)*pi/M;
FF=20*log10(abs(F));
ind=find(FF<min_dB);
FF(ind)=NaN*ones(size(ind)); % put NaN's in where
% min_dB shows up - this is to work around no clipping in xor mode
set(freq_line,'YData',FF);
set(freq_field,'String',num2str(freq));
drawnow;
elseif strcmp(action,'setwindow'),
% u = get(gcf,'userdata');
winHndl = ADDIT_DAT;
in1 = get(winHndl,'Value');
in2 = 30;
N=SIGDEMO1_DAT(3);
if (in1==1),
window = boxcar(N);
elseif (in1==2),
window = triang(N);
elseif (in1==3),
window = hanning(N);
elseif (in1==4),
window = hamming(N);
elseif (in1==5),
window = chebwin(N,30);
elseif (in1==6),
window = kaiser(N,4);
end;
SIGDEMO1_DAT(14+N:14+N+N-1)=window;
% set(gcf,'userdata',u);
sigdemo1('redraw');
if (SIGDEMO1_DAT(12)~=-1),
sigdemo1('showwind');
end;
elseif strcmp(action,'showwind'),
% u=get(gcf,'userdata');
oldfig=gcf;
N=SIGDEMO1_DAT(3);
t=SIGDEMO1_DAT(14:14+N-1);
window=SIGDEMO1_DAT(14+N:14+N+N-1);
if (SIGDEMO1_DAT(12)==-1),
SIGDEMO1_DAT(12)=figure;
axes('Position',[.15 .62 .8 .3]);
line1=plot(t,window);
title('Window function');
xlabel('time (seconds)');
grid on;
ylabel('Window');
axes('Position',[.15 .2 .8 .3]);
W=fft(window,1024);
line2=plot((0:(1/1024):(.5-(1/1024)))*N,20*log10(abs(W(1:512))));
set(gca,'xlim',[0 N/2]);
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
grid on;
windclose=uicontrol('Style','Pushbutton','Units','Normalized',...
'Position',[.85 .02 .12 .08],...
'Callback',['THEHAND=get(gcf,''userdata'');'...
'close; figure(THEHAND(1)); global SIGDEMO1_DAT, '...
'SIGDEMO1_DAT(12)=-1; clear THEHAND SIGDEMO1_DAT'],...
'String','Close');
set(gcf,'userdata',[oldfig line1 line2]);
% set(oldfig,'userdata',u);
else
figure(SIGDEMO1_DAT(12));
lines=get(gcf,'userdata');
set(lines(2),'ydata',window);
W=fft(window,1024);
set(lines(3),'ydata',20*log10(abs(W(1:512))));
drawnow
end
elseif strcmp(action,'setfreq'),
x = str2num(get(SIGDEMO1_DAT(10),'string'));
if isempty(x), % handle the non-numeric case
set(SIGDEMO1_DAT(10),'string',num2str(SIGDEMO1_DAT(1)));
else
SIGDEMO1_DAT(1)=x;
end;
elseif strcmp(action,'info'),
ttlStr = 'Discrete Fourier Transform';
hlpStr1= ...
[' '
' You are seeing discrete samples of a periodic '
' waveform (the upper plot) and the absolute '
' value of its discrete Fourier transform (DFT), '
' obtained using a fast Fourier transform (FFT) '
' algorithm (the lower plot). '
' '
' In the lower plot, frequencies from 0 to '
' 100 Hertz are displayed. The DFT at negative '
' frequencies is a mirror image of the DFT at '
' positive frequencies. The sampling rate is '
' 200 Hertz, which means the "Nyquist frequency" '
' is 100 Hertz. The DFT at frequencies above '
' the Nyquist frequency is the same as the DFT '
' at lower (negative) frequencies. '];
hlpStr2= ...
[' Click and drag a point on the waveform shown '
' in the upper plot to move that point to a new '
' location. This sets a new fundamental '
' frequency and amplitude. '
' '
' The "Signal" pop-up menu lets you change the '
' shape of the waveform. '
' '
' The "Window" menu lets you select a window '
' function. This window is multiplied by the time '
' waveform prior to taking the DFT. '
' '
' The fundamental frequency of the waveform is '
' given in the text box. You can change the '
' fundamental frequency by clicking in the text '
' box, editing the number there, and pressing '
' RETURN. You can also change the '
' fundamental by clicking and dragging on the '
' waveform. '];
hlpStr3= str2mat(...
' ', ...
' File name: sigdemo1.m');
helpfun(ttlStr, hlpStr1, hlpStr2, hlpStr3);
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -