%*********************************************************************************** % % Jim Merriam % University of Saskatchewan % jim.merriam@usask.ca % (June, 1990) % (last Oct 1998) % %*********************************************************************************** % clf disp(' CLICK ON THE FIGURE WINDOW AND FOLLOW PROMPTS') clear s; subplot(211),axis([0 10 0 10]),axis('off') s(1)={'\bf DISCRETE FOURIER TRANSFORMS I\rm'}; text(0,8,s,'FontName','times','FontSize',16),clear s; s(1)={'This is a demo on Discrete Fourier Transforms.'}; s(2)={'It demonstrates:'}; s(3)={' '}; s(4)={'HOW TO INTERPRET THE SPECTRUM'}; s(5)={'HOW TO CALCULATE THE FREQUENCY AXIS'}; s(6)={'HOW PHASE EFFECTS THE SPECTRUM'}; s(7)={' '}; text(1,2,s,'FontName','times','FontSize',10),clear s h=uicontrol('style','pushbutton', 'units','normalized','position',[.8 .01 .1 .05], ... 'string','OK','Callback','OK=''continue '';'); OK='PAUSEaBIT'; while OK=='PAUSEaBIT' pause(0.01) end clf; i=0:7; j=0:511; ft=sin(i*2*pi/4); FT=fft(ft); subplot(211) plot(i,ft,'*',j*8/512,sin(j*2*pi/256),'--'); subplot(212) axis([0 10 0 10]);axis('off'); s(1)={'Here are two complete cycles of a sine wave, sampled 8 times.'}; s(2)={'Notice that the first sample is at the zero of the sine wave.'}; s(3)={'This is important, \bf where the first sample is located in the sine'}; s(4)={'wave determines the phase of the sinusoid that will be returned'}; s(5)={'by the Discrete Fourier Transform.'}; text(1,7,s,'FontName','times','FontSize',10),clear s h=uicontrol('style','pushbutton','position',[.8 .01 .1 .05], 'units','normalized', ... 'string','OK','Callback','OK=''continue '';'); OK='PAUSEaBIT'; while OK=='PAUSEaBIT' pause(0.01) end clf; axis; subplot(211); plot(imag(FT),'*'); xlabel('m') text(1.1,.8,'f=0');text(2,.8,'fn/4');text(3,-2.9,'2fn/4');text(4,.8,'3fn/4') text(5,.8,'fn');text(6,.8,'-3fn/4'),text(7,3.1,'-2fn/4'),text(8,.8,'-fn/4') subplot(212); axis([0 10 0 10]);axis('off'); s(1)={'Here is the imaginary part of the transform. We had 8 samples of the data, '}; s(2)={'so we get 8 samples of the spectrum. The first one is the DC, the next four '}; s(3)={'are positive frequencies, and the last three are negative frequencies. '}; s(4)={'The Nyquist is at N/2+1=the fifth location. Each frequency is separated'}; s(5)={'by fn/(N/2), so you calculate the frequencies by adding this amount to'}; s(6)={'each frequency starting at the lowest positive frequency f=fn/(N/2) and'}; s(7)={'working to the right until you get fn, and then change the sign '}; s(8)={'(ie to -fn) and continue (but now you increment down from N/2, so the '}; s(9)={'frequencies reflect about the Nyquist with a change in sign). Note that we'}; s(10)={'have positive and negative frequencies everywhere except at the Nyquist'}; text(0,5,s,'FontName','times','FontSize',10),clear s h=uicontrol('style','pushbutton','position',[.8 .01 .1 .05], 'units','normalized', ... 'string','OK','Callback','OK=''continue '';'); OK='PAUSEaBIT'; while OK=='PAUSEaBIT' pause(0.01) end axis; clf; i=0:31; f1=sin(i*2*pi/16); F1=fft(f1); subplot(211) plot(i,f1,'*',j*32/512,sin(j*2*pi/256),'--'); subplot(212) axis([0 10 0 10]);axis('off'); s(1)={'Here is the same signal sampled 32 times.'}; s(2)={'Again, note that the first sample is at the zero of the'}; s(3)={'sine wave. There are four times as many samples of the same two'}; s(4)={'cycles as before, but will the spectrum of these samples reveal'}; s(5)={'anything further about the signal?'}; text(0,10,s,'FontName','times','FontSize',10),clear s h=uicontrol('style','pushbutton','position',[.8 .01 .1 .05], 'units','normalized', ... 'string','OK','Callback','OK=''continue '';'); OK='PAUSEaBIT'; while OK=='PAUSEaBIT' pause(0.01) end clf; subplot(211) plot(real(F1),'*') title('THIS IS THE REAL PART OF THE TRANSFORM'),ylabel('AMPLITUDE') xlabel('THIS IS THE FREQUENCY INDEX m') hold on; plot([1 2],[4e-15 3.e-15]) text(3,3.0e-15,'NOTICE THE SCALE!') subplot(212) hold off plot(imag(F1),'*') title('THIS IS THE IMAGINARY PART OF THE TRANSFORM'),ylabel('AMPLITUDE') xlabel('THIS IS THE FREQUENCY INDEX m') text(1,10,'Is there any further information in the spectrum?') h=uicontrol('style','pushbutton','position',[.8 .01 .1 .05], 'units','normalized', ... 'string','OK','Callback','OK=''continue '';'); OK='PAUSEaBIT'; while OK=='PAUSEaBIT' pause(0.01) end clf subplot(211) plot(imag(F1),'*') subplot(212) axis([0 10 0 10]);axis('off'); s(1)={' Here is the imaginary part again.'}; s(2)={' Where is the DC?'}; s(3)={' Where is the Nyquist?'}; s(4)={' What frequencies are represented by the two spikes?'}; s(5)={' '}; s(6)={' The amplitude of the two spikes is -16 and +16, and'}; s(7)={' the input sine was unit amplitude, so this particular'}; s(8)={' FFT has 1/N in front of the reverse transform and 1 in '}; s(9)={' front of the forward transform.'}; text(0,7,s,'FontName','times','FontSize',10),clear s h=uicontrol('style','pushbutton','position',[.8 .01 .1 .05], 'units','normalized', ... 'string','OK','Callback','OK=''continue '';'); OK='PAUSEaBIT'; while OK=='PAUSEaBIT' pause(0.01) end axis; clf m=-15:16; plot(i+1,imag(F1),'.') xlabel('m') hold on; plot(i(1)+1,imag(F1(1)),'o') text(0,3,'The DC is the first element m=1') plot(17,imag(F1(17)),'o') text(17,-3,'Here is the Nyquist m=N/2+1') plot(3,imag(F1(3)),'o') text(4,-15,'f=(m-1)*fn/(N/2)=2*fn/16') plot(31,imag(F1(31)),'o') text(15,14,'f=-(m-1)*fn/(N/2)=-2*fn/16') h=uicontrol('style','pushbutton','position',[.8 .01 .1 .05], 'units','normalized', ... 'string','OK','Callback','OK=''continue '';'); OK='PAUSEaBIT'; while OK=='PAUSEaBIT' pause(0.01) end clf hold off f1=sin((i+1)*2*pi/16); F1=fft(f1); subplot(211) plot(i+1,f1,'*',j*32/512,sin((j+1)*2*pi/256),'--'); subplot(212) axis([0 10 0 10]);axis('off'); s(1)={'Here is the same signal sampled 32 times, but this time'}; s(2)={'the first sample is one position to the right of the'}; s(3)={'zero crossing. The signal represented by the asterisks'}; s(4)={'is no longer a zero phase sine, because the first sample'}; s(5)={'is no longer at a zero crossing of the sine.'}; text(0,9,s,'FontName','times','FontSize',10),clear s h=uicontrol('style','pushbutton','position',[.8 .01 .1 .05], 'units','normalized', ... 'string','OK','Callback','OK=''continue '';'); OK='PAUSEaBIT'; while OK=='PAUSEaBIT' pause(0.01) end clf; subplot(211) plot(real(F1),'*') title('THIS IS THE REAL PART OF THE TRANSFORM'),ylabel('AMPLITUDE') xlabel('THIS IS THE FREQUENCY INDEX m') subplot(212) axis([0 10 0 10]);axis('off'); s(1)={'The real part of this spectrum is not zero, as it was in the'}; s(2)={'last spectrum. The phase lead we introduced in the '}; s(3)={'sampling is reflected in larger elements in the real'}; s(4)={'part of the spectrum. Notice that the spikes are at the same'}; s(5)={'frequencies they were at before.'}; text(0,8,s,'FontName','times','FontSize',10),clear s h=uicontrol('style','pushbutton','position',[.8 .01 .1 .05], 'units','normalized', ... 'string','OK','Callback','OK=''continue '';'); OK='PAUSEaBIT'; while OK=='PAUSEaBIT' pause(0.01) end clf;axis; subplot(211) plot(imag(F1),'*') title('THIS IS THE IMAGINARY PART OF THE TRANSFORM') ylabel('AMPLITUDE') xlabel('THIS IS THE FREQUENCY INDEX m') subplot(212); axis([0 10 0 10]);axis('off'); s(1)={'The imaginary part is not the same as before either,'}; s(2)={'some of the spike amplitude has evidently gone into'}; s(3)={'the real part. Instead of having a purely imaginary'}; s(4)={'spectrum it is now composed of real and imaginary parts.'}; s(5)={'This is because a sine with a phase lead can be '}; s(6)={'expressed as the sum of a sine and a cosine with zero'}; s(7)={'phase. The real part of a spectrum is a cosine with'}; s(8)={'zero phase, and the imaginary part a sine with zero phase'}; text(0,6,s,'FontName','times','FontSize',10),clear s h=uicontrol('style','pushbutton','position',[.8 .01 .1 .05], 'units','normalized', ... 'string','OK','Callback','OK=''continue '';'); OK='PAUSEaBIT'; while OK=='PAUSEaBIT' pause(0.01) end clf i=0:31; j=0:511; f1=sin((i+2)*2*pi/16); F1=fft(f1); subplot(211) plot(i,f1,'*'); subplot(212);axis([0 10 0 10]);axis('off'); s(1)={'Here is the same signal as the last excercise sampled 32 times again'}; s(2)={'except now the first sample is even further to the right, ie the '}; s(3)={'phase is even greater than before.'}; text(0,6,s,'FontName','times','FontSize',10),clear s h=uicontrol('style','pushbutton','position',[.8 .01 .1 .05], 'units','normalized', ... 'string','OK','Callback','OK=''continue '';'); OK='PAUSEaBIT'; while OK=='PAUSEaBIT' pause(0.01) end axis; clf; subplot(211) plot(real(F1),'*') title('THIS IS THE REAL PART OF THE TRANSFORM'),ylabel('AMPLITUDE') xlabel('THIS IS THE FREQUENCY INDEX m') hold on; s(1)={'Notice that the scale is similar in both, that is'}; s(2)={'that there is equal energy in the real and imaginary'}; s(3)={'parts, and that the peaks appear in both parts. The real'}; s(4)={'describes that part of the signal that looks like a cosine'}; text(4,6,s,'FontName','times','FontSize',10),clear s subplot(212) hold off plot(imag(F1),'*') title('THIS IS THE IMAGINARY PART OF THE TRANSFORM'),ylabel('AMPLITUDE') xlabel('THIS IS THE FREQUENCY INDEX m') s(1)={'and the imaginary part describes that part of the signal'}; s(2)={'that looks like a sine. The real and imaginary parts are equal'}; s(3)={'because the first sample was about 45 deg up the sine wave, so '}; s(4)={'the DFT sees the phase as an equal mix of sine and cosine phase'}; text(.5,10,s,'FontName','times','FontSize',10),clear s h=uicontrol('style','pushbutton','position',[.8 .01 .1 .05], 'units','normalized', ... 'string','OK','Callback','OK=''continue '';'); OK='PAUSEaBIT'; while OK=='PAUSEaBIT' pause(0.01) end clf axis([0 10 0 10]);axis('off'); s(1)={'\bf DISCRETE FOURIER TRANSFORMS I\rm'}; s(2)={''}; s(3)={' THE END'}; s(4)={' '}; text(1,9,s,'FontName','times','FontSize',16),clear s h=uicontrol('style','pushbutton','position',[.8 .01 .1 .05], 'units','normalized', ... 'string','OK','Callback','OK=''continue '';'); %OK='PAUSEaBIT'; %while OK=='PAUSEaBIT' load toocool.mat; FIGAXES=axes('position', [.28 .01 .45 .65]); image(TOOCOOL); set(FIGAXES,'visible','off') TEXTAXES=axes('position',[.05 .45 .9 .52]); set(TEXTAXES,'visible','off'); text(.28,.47,'COOL, You''re done','fontname','italic','fontsize',14); clear TOOCOOL %end pause(5) clf