10-2 Discrete Fourier Transform (?¢æ•£?…ç??‰è???

Old Chinese version

¦b«e¤@¸`¤¤¡A§Ú­Ì¥i¥H¨Ï¥Î¡uÂ÷´²®É¶¡³Å¥ß¸­Âà´«¡v¡]²ºÙ DTFT¡^¨Ó±N¤@¬q¼Æ¦ì°T¸¹Âà´«¦¨¦U­ÓÀWÃЪº¤À¶q¡A¦ý³o¬O¤@­Ó³sÄòªº¨ç¼Æ¡A¨Ã¤£¾A¦X¦b¹q¸£¤¤³B²z¡A¦]¦¹¥»¸`±N¤¶²Ð¡uÂ÷´²³Å¥ß¸­Âà´«¡v¡]Discrete Fourier Transform¡^¡A²ºÙ DFT¡A¨ä¥\¯à¬O±N¤@¬q¼Æ¦ì°T¸¹Âà´«¦¨¨ä¦U­ÓÂ÷´²ÀW²vªº©¶ªi¤À¶q¡A¥H«K«áÄò¨Ï¥Î¹q¸£¶i¦æ¦UºØ³B²z¡C

¦pªG§Ú­Ìªº°T¸¹¥i¥Hªí¥Ü¦¨ x[n], n = 0~N-1¡A¨º»ò DFT ªº¤½¦¡¦p¤U¡G

X[k]=(1/N)*Sn=0N-1 x[n]*exp(-j*2p*n*k/N), k=0, ..., N-1
³o¨Ç³Å¥ß¸­«Y¼Æ X[k] ©Ò¥Nªíªº¸ê°T¬O k ªº¨ç¼Æ¡A¦Ó k ª½±µ©MÀW²v¦³¥¿¤ñÃö«Y¡A¦]¦¹³o¨Ç«Y¼Æ X[k] ³qºÙ¬°¡uÀWÃСv¡]Spectrum¡^¡A¦Ó¹ï©ó X[k] ªº¤ÀªR¡A§Ú­Ì³qºÙ¬°¡uÀWÃФÀªR¡v¡]Spectral Analysis¡^¡C§Ú­Ì¤]¥i¥H¥Ñ³o¨Ç³Å¥ß¸­«Y¼Æ X[k]¡A¨Ó¤Ï±À­ì©l°T¸¹ x[n]¡A¦p¤U¡G
x[n]=Sk=0N-1 X[k]*exp(j*2p*n*k/N), n=0, ..., N-1

Hint
DTFT ©M DFT «ÜÃþ¦ü¡A¨âªÌ³£¥Î¨Ó³B²zÂ÷´²®É¶¡ªº°T¸¹¡A¥u¬O«eªÌ²£¥Í¤@­Ó¨¤ÀW²vªº³sÄò¨ç¼Æ¡A¦Ó«áªÌ²£¥ÍÂ÷´²ÀW²vªº°T¸¹¡A§ó¾A¦X¨Ï¥Î¹q¸£¨Ó¶i¦æ³B²z¡C

³oÃ䦳´XÂI­n»¡©ú¡G

¦pªG x[n] ¬O¹ê¼Æ¡A§Ú­Ì¥i¥H±N x[n] ªí¥Ü¦p¤U¡G
x[n] = X[0]
+ X[1]*exp(j*2p*n*1/N) + X[N-1]*exp(j*2p*n*(N-1)/N)
+ X[2]*exp(j*2p*n*2/N) + X[N-2]*exp(j*2p*n*(N-2)/N)
+ X[3]*exp(j*2p*n*3/N) + X[N-3]*exp(j*2p*n*(N-3)/N)
+ ...
¹ï¤W­z²Ä k ¶µ¦Ó¨¥¡A§Ú­Ì¦³
X[k]*exp(j*2p*n*k/N) + X[N-k]*exp(j*2p*n*(N-k)/N)
= X[k]*exp(j*2p*n*k/N) + X[N-k]*exp(j*2p*n)*exp(-j*2p*n*k/N)
= X[k]*exp(j*2p*n*k/N) + X[N-k]*exp(-j*2p*n*k/N)
= 2*Re(X[k]*exp(j*2p*n*k/N))
¦pªG§Ú­Ì¥H mk ªí¥Ü X[k] ªº¤j¤p¡A¥H pk ªí¥Ü X[k] ªº¬Û¦ì¡A¨º»ò¤W¦¡¥i¥H¤Æ²¦p¤U¡G
2*Re(mk*exp(j*pk)*exp(j*2p*n*k/N))
= 2*Re(mk*exp(j*(2p*n*k/N + pk))
= 2*mk*cos(2p*n*k/N + pk)
¤@¯ë¦Ó¨¥¡AN ¬O 2 ªº­¿¼Æ¡A¦]¦¹ x[n] ¥i¥Hªí¥Ü¦¨
x[n] = X[0] + 2*Sk=1N/2-1mk*cos(2p*n*k/N + pk) + mN/2*cos(p*n + pN/2)
´«¥y¸Ü»¡¡A§Ú­Ì¥i¥H±N­ì©l°T¸¹©î¸Ñ¦¨¤@­Óª½¬y°T¸¹ X[0] ¦A¥[¤W N/2 ­Ó©¶ªiªº²Õ¦X¡A³o¨Ç©¶ªiªº¾_´T´N¬O X[k] ªº¤j¤p¡A¦Ó¬Û¦ì«h¬O X[k] ªº¬Û¦ì¡C¦]¦¹¹ï©ó¹ê¼Æªº x[n] ¦Ó¨¥¡A§Ú­Ì¥u»Ý­n¬Ý³æÃ䪺 X[k]¡Ak = 0 ~ N/2¡A¦¹ºØ¥iºÙ¬°¡u³æÃäÀWÃСv¡C²Õ¦¨³o¨Ç³æÃäÀWÃЪº©¶ªi¦@¦³ 1+N/2 ­Ó¡AÀW²v¥Ñ¤p¨ì¤j¤À§O¬O fs/N*(0:N/2)¡A³o¨Ç©¶ªiºÙ¬°¡u°ò¥»©¶ªi¡v¡C

­Y¬O®M¥Î¤W­z¤½¦¡¨Ó­pºâ DFT¡A©Ò»Ý­nªº½ÆÂø«×¬O O(n2)¡A¦ý¦b 1965 ¦~¡A¦³¨â¦ì¾ÇªÌ´£¥X¨Ó¤@®M§óºë²ªººtºâªk¡A©Ò»Ýªº½ÆÂø«×¥u¦³ O(n log n)¡A³o¤@®MºtºâªkºÙ¬°¡u§Ö³t³Å¥ß¸­Âà´«¡v¡]Fast Fourier Transform¡A²ºÙ FFT¡^¡A´«¥y¸Ü»¡¡AFFT ¬O¥Î¨Ó­pºâ DFT ªº§Ö³t¤èªk¡C­Y¨Ï¥Î MATLAB¡A¬ÛÃöªº«ü¥O¤]¬O fft¡C

­º¥ý¡A§Ú­Ì¨Ï¥Î MATLAB ªº fft «ü¥O¨ÓÅçÃÒ¹ê¼Æ°T¸¹¤§ DFT «Y¼Æªº¦@³m©Ê¡A¦p¤U¡G

Example 1: fftSym01.m% This example demonstrates the pair-wise conjugate of DFT (¦¹½d¨Ò®i¥Ü¹ê¼Æ°T¸¹¤§ DFT «Y¼Æªº¦@³m©Ê) N=64; % Length of vector x=randn(N, 1); z=fft(x); plot(z, 'o'); grid on %compass(z); % Connect conjugate pairs (±N¤W¤U¹ïºÙªº¦@³mÂI³s°_¨Ó) for i=2:N/2+1 twoPoint=z([i, N-i+2]); line(real(twoPoint), imag(twoPoint), 'color', 'r'); end

¥Ñ¤W¹Ï¥i¥H¬Ý¥X¡ADFT «Y¼Æ·|¹ïºÙ©ó½Æ¼Æ¥­­±ªº¹ê¼Æ¶b¡A¥Nªí³o¨Ç«Y¼Æ³£·|¥H¦@³m½Æ¼Æªº¤è¦¡¦¨¹ï¥X²{¡C

Hint
¹ï©ó¹ê¼Æ°T¸¹ x[n] ¦Ó¨¥¡G
  • ·í N ¬°°¸¼Æ®É¡A¥u¦³ X[0] ©M X[N/2] ¬O³æ¤@¹ê¼Æ¡A¨ä¾l§¡¬°¦¨¹ï¥X²{¤§¦@³m½Æ¼Æ¡C
  • ·í N ¬°©_¼Æ®É¡A¥u¦³ X[0] ¬O³æ¤@¹ê¼Æ¡A¨ä¾l§¡¬°¦¨¹ï¥X²{¤§¦@³m½Æ¼Æ¡C

¦pªG x[n] «ê¥©¬O³o¨Ç°ò¥»©¶ªi¤¤ªº¨ä¤¤¤@­Ó¡A¨º»ò­pºâ¥X¨ÓªºÂùÃäÀWÃСAÀ³¸Ó¥u¦³¨â­Ó«Y¼Æ¤£¬°¹s¡A§Ú­Ì¥i¥Î MATLAB ÅçÃÒ¦p¤U¡G

Example 2: fft01.m% This example demonstrates the two-side DFT of a sinusoidal function % (¦¹½d¨Ò®i¥Ü¤@­Ó²³æ¥¿©¶ªiªº³Å¥ß¸­Âà´«¡A¥HÂùÃäÀWÃШÓÅã¥Ü) % Since the sinusoidal function has a frequency to be a multiple of fs/N, the two-side DFT have only two nonzero terms. % (¦¹¥¿©¶ªiªºÀW²v«ê¥©¬O freqStep ªº¾ã¼Æ­¿¡A©Ò¥HÂùÃäÀWÃÐÀ³¸Ó¥u¦³¨â­Ó«D¹sÂI) N = 256; % length of vector (ÂI¼Æ) fs = 8000; % sample rate (¨ú¼ËÀW²v) freqStep = fs/N; % freq resolution in spectrum (ÀW°ìªºÀW²vªº¸ÑªR«×) f = 10*freqStep; % freq of the sinusoid (¥¿©¶ªiªºÀW²v¡A«ê¬O freqStep ªº¾ã¼Æ­¿) time = (0:N-1)/fs; % time resolution in time-domain (®É°ìªº®É¶¡¨è«×) y = cos(2*pi*f*time); % signal to analyze Y = fft(y); % spectrum Y = fftshift(Y); % put zero freq at the center (±NÀW²v¶bªº¹sÂI¸m¤¤) % Plot time data subplot(3,1,1); plot(time, y, '.-'); title('Sinusoidal signals'); xlabel('Time (seconds)'); ylabel('Amplitude'); axis tight % Plot spectral magnitude freq = freqStep*(-N/2:N/2-1); % freq resolution in spectrum (ÀW°ìªºÀW²vªº¸ÑªR«×) subplot(3,1,2); plot(freq, abs(Y), '.-b'); grid on xlabel('Frequency)'); ylabel('Magnitude (Linear)'); % Plot phase subplot(3,1,3); plot(freq, angle(Y), '.-b'); grid on xlabel('Frequency)'); ylabel('Phase (Radian)');

¥Ñ¤W¹Ï¥i¥H¬Ý¥X¡G ¦pªG x[n] ªºÀW²v¤£¬O³o¨Ç©¶ªi¤¤ªº¨ä¤¤¤@­Ó¡A¨º»ò DFT ÁÙ¬O·|±N¦¹°T¸¹©î¸Ñ¦¨°ò¥»©¶ªiªº²Õ¦X¡A¦]¦¹¯à¶qÀWÃдN·|¤À´²¦b¦U­Ó°ò¥»©¶ªi¡A¦p¤U©Ò¥Ü¡G

Example 3: fft02.m% This example demonstrates the one-side DFT of a sinusoidal function (¦¹½d¨Ò®i¥Ü¤@­Ó²³æ¥¿©¶ªiªº³Å¥ß¸­Âà´«¡A¥HÂùÃäÀWÃШÓÅã¥Ü) % Since the sinusoidal function has a frequency not a multiple of fs/N, the two-side DFT smears. (¦¹¥¿©¶ªiªºÀW²v¤£¬O freqStep ªº¾ã¼Æ­¿¡A©Ò¥HÂùÃäÀWÃз|¡u´²¶}¡v(Smearing)) N = 256; % length of vector (ÂI¼Æ) fs = 8000; % sample rate (¨ú¼ËÀW²v) freqStep = fs/N; % freq resolution in spectrum (ÀW°ìªºÀW²vªº¸ÑªR«×) f = 10.5*freqStep; % freq of the sinusoid (¥¿©¶ªiªºÀW²v¡A¤£¬O freqStep ªº¾ã¼Æ­¿) time = (0:N-1)/fs; % time resolution in time-domain (®É°ìªº®É¶¡¨è«×) signal = cos(2*pi*f*time); % signal to analyze [mag, phase, freq]=fftTwoSide(signal, fs, 1); % compute and plot the two-side DFT

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î fftTwoSide.m ¨ç¼Æ¡]¦ì©ó SAP Toolbox¡^¨Ó¶i¦æÂùÃäÀWÃЪº­pºâ¡A¨Ã±N¦P®Éµe¥X®É°ì°T¸¹¡BÀWÃЯà¶q¡BÀWÃЬۦì¤T­Ó¹Ï¡C

¥Ñ©ó¹ï©ó¹ê¼Æªº x[n] ¦Ó¨¥¡AÂùÃäÀWÃЯà¶q¹Ï·|¥ª¥k¹ïºÙ¡A¦ÓÂùÃäÀWÃЬۦì¹Ï«h¬O¥ª¥k¤Ï¹ïºÙ¡A¦]¦¹§Ú­Ì¥i¥H¥u¬Ý³æÃäÀWÃЧY¥i¡A½d¨Ò¦p¤U¡G

Example 4: fft03.m% Same as fft02.m but use one-side DFT instead (¦P fft02.m¡A¦ý¥H³æÃäÀWÃШÓÅã¥Ü) N = 256; % length of vector (ÂI¼Æ) fs = 8000; % sample rate (¨ú¼ËÀW²v) freqStep = fs/N; % freq resolution in spectrum (ÀW°ìªºÀW²vªº¸ÑªR«×) f = 10.5*freqStep; % freq of the sinusoid (¥¿©¶ªiªºÀW²v¡A¤£¬O freqStep ªº¾ã¼Æ­¿) time = (0:N-1)/fs; % time resolution in time-domain (®É°ìªº®É¶¡¨è«×) signal = cos(2*pi*f*time); % signal to analyze [mag, phase, freq]=fftOneSide(signal, fs, 1); % Compute and plot one-side DFT

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î fftOneSide.m ¨ç¼Æ¡]¦ì©ó SAP Toolbox¡^¨Ó¶i¦æ³æÃäÀWÃЪº­pºâ¡A¨Ã±N¦P®Éµe¥X®É°ì°T¸¹¡BÀWÃЯà¶q¡BÀWÃЬۦì¤T­Ó¹Ï¡C

Hint
¤@¯ë¹ê»Ú¥@¬É¤¤ªº°T¸¹¡A³£¬O¹ê¼Æ°T¸¹¡A¦]¦¹§Ú­Ì¥u­n¨Ï¥Î fftOneSide.m ¨Ó­pºâ³æÃäÀWÃЧY¥i¡C

¤U­±³o­Ó½d¨Ò¡AÅã¥Ü¤@­Ó»y­µ­µ®Øªº³æÃäÀWÃСG

Example 5: fft04.m% Display DFT of a frame in a real-world audio signal (Åã¥Ü¤@­Ó»y­µ­µ®Øªº³æÃäÀWÃÐ) au=myAudioRead('welcome.wav'); frame=au.signal(2047:2047+237-1); [mag, phase, freq]=fftOneSide(frame, au.fs, 1);

¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¥i¥H¬Ý¨ì«Ü©úÅ㪺¿Óªi¥X²{¦bÀWÃЯà¶q¡A³o¬O¥Ñ©ó°T¸¹ªº¶g´Á©Ê©Ò³y¦¨ªºµ²ªG¡C¡]¬°¤F¥YÅã¿Óªi²{¶H¡A§Ú­Ìªº­µ®Ø­è¦n¥]§t¤F¤T­Ó§¹¾ãªº°ò¥»¶g´Á¡C¡^

·í k ¶V¤j®É¡A¥Nªí¹ïÀ³ªº©¶ªi¬O°ªÀW°T¸¹¡A¦]¦¹§Ú­Ì¥i¥H±Ë±ó°ªÀW°T¸¹¡A¥u¥Î´X­Ó§CÀW©¶ªi¡A¨Ó¦X¦¨­ì©l°T¸¹¡A¹F¨ì¤U¦C¨â¶µ¥Øªº¡G

¥H¤U³o­Ó½d¨Ò¡AÅã¥Ü¦p¦ó¥H©¶ªi¨Ó¦X¦¨¤@­Ó¤èªi¡G

Example 6: fftApproximate01.m% This example demos the effect of square wave approximation by DFT figure L = 15; N = 25; x = [ones(1,L), zeros(1,N-L)]; frameSize=length(x); runNum=3; for i=1:runNum, pointNum=ceil(frameSize/(2*runNum)*i); % Actually 2*pointNum-1 coefs are taken X = fft(x); magX = abs(X); remainIndex=[1:pointNum, frameSize-pointNum+2:frameSize]; X2=0*X; X2(remainIndex)=X(remainIndex); x2=ifft(X2); x2=real(x2); subplot(3,2,2*i-1); stem(x); hold on plot(x2, 'r'); hold off title(sprintf('x[n] and %d-points approximation', 2*pointNum-1)); axis([-inf,inf,-0.5,1.5]) subplot(3,2,2*i); shiftedMagX=fftshift(magX); plot(shiftedMagX, '.-'); axis tight title('DFT of x[n]') hold on temp=ifftshift(1:frameSize); ind=temp(remainIndex); plot(ind, shiftedMagX(ind), 'or'); grid on hold off end

¥Ñ¦¹¥i¨£·í§Ú­Ì©Ò¥Îªº DFT «Y¼Æ¶V¦h¡A©Ò¦X¦¨ªºªi§Î´N·|¶V±µªñ­ì¨Óªºªi§Î¡C

¤U­±³o­Ó½d¨Ò¡A«h¬O¥H©¶ªi¨Ó¦X¦¨¤@­Ó¹ê»Ú»¡¸ÜÁn­µªºªi§Î¡G

Example 7: fftApproximate02.m% Display the effect of FFT approximation au=myAudioRead('welcome.wav'); x=au.signal(2047:2047+237-1); figure frameSize=length(x); runNum=3; for i=1:runNum, pointNum=ceil(frameSize/(8*runNum)*i); % Actually 2*pointNum-1 coefs are taken X = fft(x); magX = abs(X); remainIndex=[1:pointNum, frameSize-pointNum+2:frameSize]; X2=0*X; X2(remainIndex)=X(remainIndex); x2=ifft(X2); x2=real(x2); subplot(3,2,2*i-1); plot(x, '.-'); hold on plot(x2, 'r'); hold off title(sprintf('x[n] and %d-points approximation', 2*pointNum-1)); set(gca, 'xlim', [-inf inf]); subplot(3,2,2*i); shiftedMagX=fftshift(magX); plot(shiftedMagX, '.-'); title('DFT of x[n]') hold on temp=ifftshift(1:frameSize); ind=temp(remainIndex); plot(ind, shiftedMagX(ind), 'or'); grid on hold off set(gca, 'xlim', [-inf inf]); end

¥Ñ¦¹¥i¨£¦b¹ê»ÚªºÁn­µªi§Î¤¤¡A§Ú­Ì¥u­n¤Ö¼Æ´X­Ó§CÀWªº«Y¼Æ¡A´N¥i¥H¦X¦¨Ãþ¦ü­ì¨Óªº°T¸¹¡A¥Ñ¦¹¤]¥i¥Hª¾¹D«Ü¦h°ªÀW°T¸¹¬O¨S¦³§@¥Îªº¡C

¹ï©ó¶g´Á©Êªºªi§Î¡ADFT ·|´¡¤J¹sÂI¡A¦p¤U¡G

Example 8: fftRepeat01.m% This example demos the effect of FFT for purely periodic signals au=myAudioRead('welcome.wav'); x=au.signal(2047:2126); % A full fundamental period runNum=5; for i=1:runNum repeatedX = x*ones(1,i); signal = repeatedX(:)+0.1; % signal=zeros(runNum*length(x), 1); % Zero-padding version % signal(1:length(repeatedX))=repeatedX(:); % Zero-padding version [mag, phase, freq, powerDb]=fftOneSide(signal, au.fs); mag=mag/length(signal); % Divided by vector length to normalize magnitude (due to the formula used by MATLAB) subplot(runNum,2,2*i-1); plot(signal, '.-'); grid on title('x[n]'); set(gca, 'xlim', [-inf inf]); subplot(runNum,2,2*i); plot(freq, mag, '.-'); grid on; % set(gca, 'yscale', 'log'); title('DFT of x[n]'); axis tight; end

¬°¤°»ò DFT ·|´¡¤J¹sÂI©O¡H­Y¤£¨Ï¥Î¸Ô²Óªº¼Æ¾Ç¤ÀªR¡A¦U¦ì¦P¾Ç¬O§_¥i¥H¥Hª½Ä±ªº¤è¦¡¨Ó±À¾É¥X³o­Óµ²ªG©O¡H

­Y¦b®É°ìªi§Î¥[¤W¹sÂI¡A«h¦bÀW°ì©Ò²£¥Íªº®ÄªG¬O¤º®t¥H¼W¥[ÂI¼Æ¡A¦p¤U©Ò¥Ü¡G

Example 9: fftZeroPadding01.m% This example demos the effect of zero-padding of DFT for i=1:3 L = 5; N = 20*i; x = [ones(1,L), zeros(1,N-L)]; subplot(3,3,i*3-2); stem(x); title(sprintf('x[n] with N=%d',N)); set(gca, 'xlim', [-inf inf]); omega=((1:N)-ceil((N+1)/2))*(2*pi/N); X = fft(x); magX = fftshift(abs(X)); subplot(3,3,i*3-1); plot(omega, magX, '.-'); title('Magnitude of DFT of x[n]') set(gca, 'xlim', [-inf inf]); phase=fftshift(angle(X)); subplot(3,3,i*3); plot(omega, phase, '.-'); title('Phase of DFT of x[n]') set(gca, 'xlim', [-inf inf]); set(gca, 'ylim', [-pi pi]); end

´«¥y¸Ü»¡¡A¥[¤W¹sÂI¡A¦b¹ê½è¤W¨Ã¨S¦³¼W¥[°T¸¹ªº¸ê°T¡A¦ý¬O DFT ªºÂI¼Æ¥²¶·ÀH¤§¼W¥[¡A¦]¦¹¦bÀWÃЪº®ÄÀ³«h¬O¶i¦æ¤º®t¥H¼W¥[ÂI¼Æ¡C

Hint
¤@¯ë¦b¶i¦æ­µ°T³B²z®É¡A¦pªG­µ®ØªºÂI¼Æ¤£¬O 2n ªº®æ¦¡¡A§Ú­Ì³q±`´N¬O¨Ï¥Î¸É¹sªº¤è¦¡¡A¨Ï³Ì«áªºÂI¼ÆÅܦ¨ 2n¡A³o¼Ë¦b¶i¦æ DFT ®É¡A¤~¤ñ¸û¤è«K±Ä¨ú¤ñ¸û§Ö³tªº FFT ­pºâ¤è¦¡¡C

­Y¦b®É°ì¶i¦æ Downsample¡A«h¦bÀW°ì©Ò²£¥Íªº®ÄªG¦p¤U¡G

Example 10: fftReSample01.m% This example demos the effect of FFT approximation au=myAudioRead('welcome.wav'); x=au.signal(2047:2126); x=au.signal(2047:2326); n=length(x); F = (0:n/2)*au.fs/n; runNum=5; for i=1:runNum, newX=x(1:2^(i-1):length(x)); newFs=au.fs/(2^(i-1)); X = fft(newX); magX = abs(X); frameSize=length(newX); subplot(runNum,2,2*i-1); plot(newX, '.-'); title('x[n]'); set(gca, 'xlim', [-inf inf]); subplot(runNum,2,2*i); freq = (0:frameSize/2)*newFs/frameSize; magX = magX(1:length(freq)); M=nan*F; M(1:length(magX))=magX; plot(F, M, '.-'); title('DFT of x[n]') axis tight; end

·í§Ú­Ì Downsample ¶i¦æ¶V¦h¦¸¡A¦±½u·|¶V¥­·Æ¡A¥Nªí°ªÀW³¡¤À¤w¸gºCºC¤£¨£¤F¡I

¦¹¥~¡A§Ú­Ì¥i¥H±N­ì¥ýªº®É°ì°T¸¹ªí¥Ü¦¨¼Æ­Ó©¶ªi¨ç¼Æªº½u©Ê²Õ¦X¡A¨Ã¨Ï¥Î³Ì¤p¥­¤èªk¡]lease-squares estimate¡^¨Ó¶i¦æ¦±½uÀÀ¦X¡]Curve fitting¡^¥H§ä¥X³Ì¨Îªº½u©Ê«Y¼Æ¡A³o¨Ç½u©Ê«Y¼Æ©M fft ©Ò±o¨ìªºµ²ªG¬O¤@¼Ëªº¡A½d¨Ò¦p¤U¡G

Example 11: fftViaLse01.m% FFT via least-squares estimate N=8; fs=1; time=(0:N-1)'/fs; x=rand(N,1)*2-1; A=ones(N,1); for i=1:N/2 A=[A, cos(2*pi*(i*fs/N)*time), sin(2*pi*(i*fs/N)*time)]; end th=A\x; plotNum=fix(N/2)+2; subplot(plotNum, 1, 1); N2=(N-1)*5+1; % Insert 4 points between every 2 points for better observation (¨âÂI¶¡´¡¤J¥|ÂI¡A¥H«KÆ[¹îªi§Î) timeFine=linspace(min(time), max(time), N2); x2=th(1)*ones(N,1); plot(timeFine, th(1)*ones(N2,1), '.-', time, x2, 'or'); % Plot the first term (µe¥X²Ä¤@¶µ) ylabel('Term 0 (DC)'); axis([0 N/fs -1 1]); grid on for i=1:N/2 % Plot terms 2 to 1+N/2 (µe¥X²Ä¤G¦Ü²Ä 1+N/2 ¶µ) freq=i*fs/N; y=th(2*i)*cos(2*pi*freq*time)+th(2*i+1)*sin(2*pi*freq*time); % a term (¨C¤@¶µ) x2=x2+y; fprintf('i=%d, sse=%f\n', i, norm(x-x2)/sqrt(N)); subplot(plotNum, 1, i+1); yFine=th(2*i)*cos(2*pi*(i*fs/N)*timeFine)+th(2*i+1)*sin(2*pi*(i*fs/N)*timeFine); % Finer verison for plotting plot(timeFine, yFine, '.-', time, y, 'or'); ylabel(sprintf('Term %d', i)); axis([0 N/fs -1 1]); grid on end % Plot the original signal (µe¥X­ì¨Ó°T¸¹) subplot(plotNum, 1, plotNum) plot(time, x, 'o-'); axis([0 N/fs -1 1]); grid on xlabel('Time'); ylabel('Orig signals'); % Transform LSE result back to fft format for comparison (±N th Âà¦^ fft ¨Ã¤ñ¸ûµ²ªG) F=fft(x); F2=[]; F2(1)=th(1)*N; for i=1:N/2 F2(i+1)=(th(2*i)-sqrt(-1)*th(2*i+1))*N/2; if (i==N/2) F2(i+1)=2*F2(i+1); end end % symmetric of DFT (DFT ªº¹ïºÙ©Ê) for i=N/2+2:N F2(i)=F2(N-i+2)'; end error1=sum(abs(F2-F.')); % F.' is simple transpose (F.' ¬O¤£¶i¦æ¦@³mÂà´«ªºÂà¸m) error2=sum(abs(F2-F')); % F' is conjugate transpose (F' ¬O¶i¦æ¦@³mÂà´«ªºÂà¸m) fprintf('Errors after transforming LSE to DFT coefficients (±N LSE Âà´«¦¨ DFT «Y¼Æªº»~®t)¡Gerror1=%f, error2=%f\n', error1, error2); fprintf('Due to the symmetry of DFT, one of the above error terms should be zero. (¥Ñ©ó DFT ªº¹ïºÙ©Ê¡A¤W­z»~®tÀ³¸Ó¦³¤@¶µ¬°¹s¡C)\n');i=1, sse=0.626965 i=2, sse=0.306336 i=3, sse=0.008109 i=4, sse=0.000000 Errors after transforming LSE to DFT coefficients (±N LSE Âà´«¦¨ DFT «Y¼Æªº»~®t)¡Gerror1=0.000000, error2=22.709014 Due to the symmetry of DFT, one of the above error terms should be zero. (¥Ñ©ó DFT ªº¹ïºÙ©Ê¡A¤W­z»~®tÀ³¸Ó¦³¤@¶µ¬°¹s¡C)


Audio Signal Processing and Recognition (­µ°T³B²z»P¿ëÃÑ)