Friday, October 14, 2011

FFT dari Sinyal Sinus di Matlab/Octave

Seharusnya, bila kita membuat sinyal sinusoidal dengan frekuensi 20 Hz, maka kalau kita fft-kan (fourrier transform-kan) sinyal tersebut memiliki frekuensi tunggal pada sumbu X=20. Bila tidak, atau plotnya menunjukkan spektrum yang tidak beraturan, maka gambar dan teknik yang kita gunakan tersebut salah.
Sinyal Sin 20 Hz dan FFT -nya
Cek dan run listing code matlab di bawah untuk menghasilkan FFT dari sinyal sinus yang match dengan frekuensinya.
  1. % Sampling frequency
  2. Fs = 1024; 
  3. % Time vector of 1 second
  4. t = 0:1/Fs:1;
  5. % Create a sine wave of 20 Hz.
  6. x = sin(2*pi*t*20);
  7. % Use next highest power of 2 greater than or equal to length(x) to calculate FFT.
  8. nfft= 2^(nextpow2(length(x)));

  9. % Take fft, padding with zeros so that length(fftx) is equal to nfft
  10. fftx = fft(x,nfft);

  11. % Calculate the numberof unique points
  12. NumUniquePts = ceil((nfft+1)/2);

  13. % FFT is symmetric, throw away second half
  14. fftx = fftx(1:NumUniquePts);

  15. % Take the magnitude of fft of x and scale the fft so that it is not a function of the length of x
  16. mx = abs(fftx)/length(x);

  17. % Take the square of the magnitude of fft of x.
  18. mx = mx.^2;

  19. % Since we dropped half the FFT, we multiply mx by 2 to keep the same energy.
  20. % The DC component and Nyquist component, if it exists, are unique and should not be multiplied by 2.
  21. if rem(nfft, 2) % odd nfft excludes Nyquist point
  22.   mx(2:end) = mx(2:end)*2;
  23. else
  24.   mx(2:end -1) = mx(2:end -1)*2;
  25. end

  26. % This is an evenly spaced frequency vector with NumUniquePts points.
  27. f = (0:NumUniquePts-1)*Fs/nfft;

  28. % Generate the plot, title and labels.
  29. subplot(211); plot(x);
  30. title('Waveform of a 20Hz Sine Wave');
  31. xlabel('Time');
  32. ylabel('Amplitude');
  33. subplot(212); plot(f,mx);
  34. title('Power Spectrum of a 20Hz Sine Wave');
  35. xlabel('Frequency (Hz)');
  36. ylabel('Power');
Source: Mathwork
Related Posts Plugin for WordPress, Blogger...