📄 signt.m
字号:
elseif (val == 2), % sawtooth
for i=1:N
if i==N/2, f(i)=1;
else f(i)=0;
end;
end
%tt=freq1*t*2*pi;
% f=amp1*((tt < 0) + rem(tt,2*pi)/2/pi - .5)*2;
elseif (val == 4), % self-defined waveform
f=eval(fun_str);
end;
set(time_line,'YData',f);
F=fft(f,M);
%F=F(1:M);
w=(0:M-1)*ws/(2*M);
FF=abs(F);
F=fft(f,M);
%F=F(1:M);
w=2*pi/ws/M*(0:M-1);
FF=sqrt(F.*conj(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
freq_line=plot(w,FF,'EraseMode','normal');
axis([0 10 0 15]);
grid;
ylabel('幅频特性');
xlabel('频率 (Hz)');
ax_time=axes('Position',[.12 .58 .6 .3],'XLim',[-10 10],'YLim',[0 1]);
stem(t,f);
axis([-10 10 0 1]);
% (set to xor mode to prevent re-rendering, that is, for speed)
grid;
ylabel('波形');
xlabel('时间(秒)');
% 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=signt_DAT(1);
amp=signt_DAT(2);
fixed_x=signt_DAT(6);
fixed_y=signt_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(signt_DAT(8),'linestyle','-','markersize',8);
signt_DAT(1)=freq1; % set amplitude and frequency
signt_DAT(2)=amp1;
% set(gcf,'userdata',u);
signt('redraw');
elseif strcmp(action,'done'),
% close the figure window that is showing the window fnction:
% u = get(gcf,'userdata');
show_window=findobj('Tag','window_window');
if ( ~isempty(show_window) & signt_DAT(12)~=-1 )
close(signt_DAT(12));
end;
close(gcf);
clear global signt_DAT
clear global ADDIT_DAT
clear global fun_str
clear show_window
elseif strcmp(action,'redraw'),
% recomputes time and frequency waveforms and updates display
% u = get(gcf,'userdata');
freq=signt_DAT(1);
zq=signt_DAT(2);
N=signt_DAT(3);
M=signt_DAT(4);
zq_freq=signt_DAT(5);
time_line=signt_DAT(8);
freq_line=signt_DAT(9);
freq_field=signt_DAT(10);
popup=signt_DAT(11);
%ax_freq=axes('Position',[.12 .14 .6 .3],'XLim',...
% [0,10],'YLim',[0 10]);
% plot(w,FF,'EraseMode','normal');
N1=20;%M=512;
t=-30:30;
ws=t(2)-t(1);
% window=signt_DAT(14+N:14+N+N-1);
val = get(popup,'Value');
% if(freq<0.05)freq=0.05;end
% if(freq>10)freq=10;end
%if(amp>1.0)amp=1.0;end
% if (val == 1),
% if t==0,f=1
% else
%f=amp*sin(freq*t)./(t*pi);
% end;
if (val == 1), % square wave
i=-30:30;
for n=1:61
j=fix(n/zq);
if abs(n-j*zq-zq/2-1)<=freq
f(n)=1;
else
f(n)=0;
end
end
%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 == 2), % sawtooth
%for i=1:N
if t==0 f=1;
else f=0;
end
%end
%tt=freq*t*2*pi;
%f=amp*((tt < 0) + rem(tt,2*pi)/2/pi - .5)*2;
elseif (val == 3), % self-defined waveform
f=eval(fun_str);
end;
F=fft(f,M);
%F=F(1:M);
w=2*pi/ws/M*(0:M-1);
FF=sqrt(F.*conj(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
% plot(w,FF,'EraseMode','xor');
% plot(w,FF,'y');
% axis([0 10 0 15]);
% grid;
% ylabel('幅频特性');
%xlabel('频率 (Hz)');
%ax_time=axes('Position',[.12 .58 .6 .3],'XLim',[-10 10],'YLim',[0 1]);
stem(t,f);
axis([-30 30 0 1]);
% (set to xor mode to prevent re-rendering, that is, for speed)
grid;
ylabel('波形');
xlabel('时间(秒)');
% freq_line=plot(w,FF,'EraseMode','xor');
% plot(w,FF);
% axis([0 10 0 15]);
%set(time_line,'YData',f);
% F=fft(f,2*M);
%F=F(1:M);
%w=(0:M-1)*ws/(2*M);
%FF=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
% freq_line=plot(w1(1:N/2),FF(1:N/2),w2(1:N/2),FF(1:N/2),'EraseMode','xor');
% axis([-100 100 0 40]);
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=signt_DAT(3);
%show_window=signt_DAT(14+N+N);
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;
signt_DAT(14+N:14+N+N-1)=window;
% set(gcf,'userdata',u);
signt('redraw');
if get(show_window,'value'),
signt('showwin');
end;
elseif strcmp(action,'showwin'),
% u=get(gcf,'userdata');
oldfig=gcf;
N=signt_DAT(3);
% t=signt_DAT(14:14+N-1);
%window=signt_DAT(14+N:14+N+N-1);
if (signt_DAT(12)==-1),
signt_DAT(12)=figure('NumberTitle','off', ...
'Name','窗口', 'menubar','none',...
'Tag','window_window');
axes('Position',[.15 .62 .8 .3]);
line1=plot(t,window);
title('窗口函数');
xlabel('时间(秒)');
grid;
ylabel('窗口');
axes('Position',[.15 .2 .8 .3]);
W=fft(window,1024);
line2=plot((0:(1/1024):(.5-(1/1024)))*N,abs(W(1:512)));
set(gca,'xlim',[0 N/2]);
xlabel('频率 (Hz)');
ylabel('幅度 (dB)');
grid;
windclose=uicontrol('Style','Pushbutton',...
'Position',[.85 .02 .12 .08],...
'Units','normalized',...
'String','关闭',...
'Callback',[...
'THEHAND=get(gcf,''userdata'');',...
'close;',...
'figure(THEHAND(1));',...
'global signt_DAT;',...
'signt_DAT(12)=-1;',...
'clear THEHAND signt_DAT;']);
set(gcf,'userdata',[oldfig line1 line2]);
% set(oldfig,'userdata',u);
clear line1 line2 W;
else
show_window=findobj('Tag','window_window');
if isempty(show_window)
signt_DAT(12)=-1;
signt('showwin');
else
figure(signt_DAT(12));
drawnow
lines=get(gcf,'userdata');
set(lines(2),'ydata',window);
W=fft(window,1024);
set(lines(3),'ydata',abs(W(1:512)));
figure(signt_DAT(12));
drawnow;
clear W lines;
end
clear show_window;
end
clear oldfig N t window;
elseif strcmp(action,'delshow'),
N=signt_DAT(3);
show_window=signt_DAT(14+N+N);
if ( signt_DAT(12)~=-1 & get(show_window,'value')==0),
show_window=findobj('Tag','window_window');
if isempty(show_window)
signt_DAT(12)=-1;
else
figure(show_window);
THEHAND=get(gcf,'userdata');
close;
figure(THEHAND(1));
signt_DAT(12)=-1;
clear THEHAND;
end
end
clear show_window N;
elseif strcmp(action,'setzq'),
x = str2num(get(signt_DAT(5),'String'));
if isempty(x), % handle the non-numeric case
set(signt_DAT(5),'String',num2str(signt_DAT(2)));
else
signt_DAT(2)=x;
end;
elseif strcmp(action,'setfreq'),
x = str2num(get(signt_DAT(10),'String'));
if isempty(x), % handle the non-numeric case
set(signt_DAT(10),'String',num2str(signt_DAT(1)));
else
signt_DAT(1)=x;
end;
elseif strcmp(action,'info'),
ttlStr = '离散周期信号傅立叶变换(DFT)';
hlpStr1= str2mat(...
' ',...
' 我们看到,在演示窗口中,上方的图表示了一个离 ',...
' 散时间周期信号的时域波形。可通过左边的周期文 ',...
' 本框或离散点数文本框来改变时域信号的周期或波 ',...
' 形点数。 ',...
' ',...
' 下方的图给出了该信号经离散傅立叶变换(DFT)后' ,...
' 的频谱图。DFT的值由快速傅立叶变换算法(FFT) ' ,...
' 得到。 ' ,...
' ' ,...
' 注意:下图中,频域图形是以2п即2*3.14为周期的' ,...
' 在此只画出了一个周期内的频谱特性。负频域的DFT',...
' 是正频域DFT的镜象,故在此只画出了正频域的DFT ' ,...
' ');
hlpStr2=str2mat(...
' ' ,...
' 左边的文本框给出了信号的周期或点数,可以通过用 ',...
' 鼠标点击来选中编辑文本框,然后用键盘输入信号的 ',...
' 周期或点数,再回车就可改变信号的周期或离散点数。' ,...
' ',...
' 从图中所示的波形可看出:当通过文本框来改变离散 ',...
' 信号的点数或周期时,时域信号与频域信号的波形都 ',...
' 相应有所改变。同学们应对时域信号与频域信号的变 ',...
' 化关系予以解释。 ',...
' 时域信号的周期不管如何改变,频域信号在一个周期 ',...
' 内频谱起伏的个数与信号周期的大小有关。 ',...
' 频域信号是以2п即2*3.14为周期的。 ' );
helpwin(ttlStr, hlpStr1, hlpStr2);
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -