📄 sigan.m
字号:
val = get(popup,'Value');
%if(freq1<0.05)freq1=0.05;end
%if(freq>0.95)freq1=0.95;end
% if(amp1>1.0)amp1=1.0;end
%if (val == 1),
%if t==0 f=1;
%else
% f=amp1*sin(freq1*t)./(t*pi);
% end;
if (val == 1), % square wave
for i=1:N1+1
if abs(i-N1/2-1)<=freq1
f(i)=amp1;
else
f(i)=0;
end
end
%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 == 2), % sawtooth
for i=1:N1+1
if i==(N1/2)+1, 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=sigan_DAT(1);
amp=sigan_DAT(2);
fixed_x=sigan_DAT(6);
fixed_y=sigan_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(sigan_DAT(8),'linestyle','-','markersize',8);
sigan_DAT(1)=freq1; % set amplitude and frequency
sigan_DAT(2)=amp1;
% set(gcf,'userdata',u);
sigan('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) & sigan_DAT(12)~=-1 )
close(sigan_DAT(12));
end;
close(gcf);
clear global sigan_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=sigan_DAT(1);
amp=sigan_DAT(2);
N=sigan_DAT(3);
M=sigan_DAT(4);
min_dB=sigan_DAT(5);
time_line=sigan_DAT(8);
freq_line=sigan_DAT(9);
freq_field=sigan_DAT(10);
popup=sigan_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=-10:10;
ws=t(2)-t(1);
% window=sigan_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
for i=1:N1+1
if abs(i-N1/2-1)<=freq
f(i)=1;
else
f(i)=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:N1+1
if i==(N1/2)+1, f(i)=1;
else f(i)=0;
end
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([-10 10 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=sigan_DAT(3);
%show_window=sigan_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;
sigan_DAT(14+N:14+N+N-1)=window;
% set(gcf,'userdata',u);
sigan('redraw');
if get(show_window,'value'),
sigan('showwin');
end;
elseif strcmp(action,'showwin'),
% u=get(gcf,'userdata');
oldfig=gcf;
N=sigan_DAT(3);
% t=sigan_DAT(14:14+N-1);
%window=sigan_DAT(14+N:14+N+N-1);
if (sigan_DAT(12)==-1),
sigan_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 sigan_DAT;',...
'sigan_DAT(12)=-1;',...
'clear THEHAND sigan_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)
sigan_DAT(12)=-1;
sigan('showwin');
else
figure(sigan_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(sigan_DAT(12));
drawnow;
clear W lines;
end
clear show_window;
end
clear oldfig N t window;
elseif strcmp(action,'delshow'),
N=sigan_DAT(3);
show_window=sigan_DAT(14+N+N);
if ( sigan_DAT(12)~=-1 & get(show_window,'value')==0),
show_window=findobj('Tag','window_window');
if isempty(show_window)
sigan_DAT(12)=-1;
else
figure(show_window);
THEHAND=get(gcf,'userdata');
close;
figure(THEHAND(1));
sigan_DAT(12)=-1;
clear THEHAND;
end
end
clear show_window N;
elseif strcmp(action,'setfreq'),
x = str2num(get(sigan_DAT(10),'String'));
if isempty(x), % handle the non-numeric case
set(sigan_DAT(10),'String',num2str(sigan_DAT(1)));
else
sigan_DAT(1)=x;
end;
elseif strcmp(action,'info'),
ttlStr = '离散非周期信号傅立叶变换(DFT)';
hlpStr1=str2mat(...
' ',...
' 我们看到,在演示窗口中,上方的图表示了一个离 ' ,...
' 散非周期信号的时域波形。用鼠标单击“时限长度”',...
' 的文本框选中输入值,用键盘输入数值即可改变离散',...
' 信号的点数。 ',...
' 离散信号的点数就等于所输入的数值。 ' ,...
' ' ,...
' 下方的图给出了该信号经离散傅立叶变换后的频谱',...
' 图即显示了信号的频域特性。 ' ,...
' ' ,...
' 注意:下图中,频域图形是以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 + -