MATLAB2_lezione9

Embed Size (px)

Citation preview

  • 8/13/2019 MATLAB2_lezione9

    1/16

    Trasformata di Fourier continua:

    f( )= f(t) exp(-2 i t) dt Consideriamo ora una funzione discreta f k=f(tk) con

    t k =k* , k =0,, N -1. La trasformata di Fourier discreta(DFT) definita da:

    F n= k=0 N-1 f k exp(- 2 i n k/N) La FFT riduce il numero di calcoli necessari per N punti

    da 2 N 2 a 2 N log( N ). La funzione fft fornisce la DFT y di un vettore input x

    usando lalgoritmo FFT: y = fft(x); La lunghezza della trasformata pu essere specificata

    da un secondo argomento opzionale: y = fft(x,n);

    9. FAST FOURIER TRANSFORM (FFT)

  • 8/13/2019 MATLAB2_lezione9

    2/16

  • 8/13/2019 MATLAB2_lezione9

    3/16

    Per esempio consideriamo i dati x con due frequenze di diversaampiezza e un rumore:fs = 100; % Sample frequency (Hz)t = 0:1/fs:10-1/fs; % 10 sec sample

    x = (1.3)*sin(2*pi*15*t) ... % 15 Hz component+ (1.7)*sin(2*pi*40*(t-2)) ... % 40 Hz component+ (2.5)*randn(size(t)); % Gaussian noise;

    Usare fft per calcolare la DFT y:m = length(x); % Window lengthn = pow2(nextpow2(m)); % Transform lengthy = fft(x,n); % DFTf = (0:n-1)*(fs/n); % Frequency range

    power = y.*conj(y)/n; % Power of the DFTnextpow2 trova lesponente della potenza di 2 della windowlength (ceil(log2(m)))

    pow2 calcola la potenza.

    http://www.mathworks.com/help/techdoc/ref/nextpow2.htmlhttp://www.mathworks.com/help/techdoc/ref/pow2.htmlhttp://www.mathworks.com/help/techdoc/ref/pow2.htmlhttp://www.mathworks.com/help/techdoc/ref/nextpow2.html
  • 8/13/2019 MATLAB2_lezione9

    4/16

    Per visualizzare la DFT si usano i plot di abs( y ),abs( y ).^2, and log(abs( y )) . Un plot della potenza versus frequenza si chiama

    periodogramma : plot(f,power) xlabel('Frequency (Hz)')ylabel('Power')title('{\bf Periodogram}')

    In molte applicazioni si usa centrare il periodogrammain 0. La funzione fftshift arrangia loutput di fft per

    produrre un periodogramma centrato in 0:y0 = fftshift(y); % Rearrange y valuesf0 = (-n/2:n/2-1)*(fs/n); % 0-centered frequency range

    power0 = y0.*conj(y0)/n; % 0-centered power plot(f0,power0)title('{\bf 0-Centered Periodogram}')

  • 8/13/2019 MATLAB2_lezione9

    5/16

  • 8/13/2019 MATLAB2_lezione9

    6/16

    Costruiamo il segnale yvn sovrapponendo ad un segnalecon un contenuto in frequenza di 50 Hz, che chiamiamo yv,

    del rumore casuale a media nulla (prendiamo 27 intervalli):

    >> omega=2*pi*50;>> y0=2*pi/16;

    >> N=length(tspan);>> f=128/N*(0:N/2-1);>> yv=y0*cos(omega*tspan);>> yvn=yv+4*randn(size(tspan));

    Analizziamo il segnale misurato nel dominio dellefrequenze, calcolando la FFT di yvn .

  • 8/13/2019 MATLAB2_lezione9

    7/16

    In generale, se y ha N componenti, il comando fft(y) calcola i coefficienti dello sviluppo di Fourier secondo la

    relazione:

    F(k) = j=1 N y(j) N (j-1)(k-1) , dove N = e -2 /N .

    >> YVN=fft(yvn);

    calcoliamo la densit di energia spettrale, definita come:P= k=1 N (F(k) * F(k) c ) / N ;

    dove F(k) c indica il coniugato di F(k) :>> PVN=YVN.*conj(YVN)/N;>> plot(f,PVN(1:N/2))(spettro della densit di energia del segnale)

  • 8/13/2019 MATLAB2_lezione9

    8/16

    Valutiamo con max il valore della frequenza dove siregistra il massimo della densit di energia spettrale:

    >> [y,j]=max(PVN);>> f(j)

    ans=

    50

    ovvero il massimo di energia viene registrato per unafrequenza pari a 50 Hz.

  • 8/13/2019 MATLAB2_lezione9

    9/16

    EXAMPLE from MATHWORKS

    A common use of Fourier transforms is to find thefrequency components of a signal buried in a noisy time

    domain signal.

    Consider data sampled at 1000 Hz. Form a signalcontaining a 50 Hz sinusoid of amplitude 0.7 and 120 Hz

    sinusoid of amplitude 1 and corrupt it with some zero-mean random noise.

  • 8/13/2019 MATLAB2_lezione9

    10/16

  • 8/13/2019 MATLAB2_lezione9

    11/16

    It is difficult to identify the frequency components by looking at the originalsignal. Converting to the frequency domain, the discrete Fourier transformof the noisy signal y is found by taking the fast Fourier transform (FFT):

    NFFT = 2^nextpow2(L); % Next power of 2 from length of y Y = fft(y,NFFT)/L; f = Fs/2*linspace(0,1,NFFT/2+1); % Plot single-sided amplitude spectrum. plot(f,2*abs(Y(1:NFFT/2+1))) title('Single-Sided Amplitude Spectrum of y(t)') xlabel('Frequency (Hz)')

    ylabel('|Y(f)|') The main reason the amplitudes are not exactly at 0.7 and 1 is because ofthe noise. Several executions of this code (including recomputation of y) willproduce different approximations to 0.7 and 1. The other reason is that youhave a finite length signal. Increasing L from 1000 to 10000 in the exampleabove will produce much better approximations on average.

  • 8/13/2019 MATLAB2_lezione9

    12/16

  • 8/13/2019 MATLAB2_lezione9

    13/16

    Here is a closer look at the first 50 years. plot(year(1:50),relNums(1:50),'b.-');

    The first component of Y, Y(1), is simply the sum of thedata, and can be removed.

    Y = fft(relNums); Y(1)=[];

    A graph of the distribution of the Fourier coefficients(given by Y) in the complex plane is pretty, but difficult tointerpret. We need a more useful way of examining the

    data in Y. plot(Y,'ro') title('Fourier Coefficients in the Complex Plane');

    xlabel('Real Axis');

    ylabel('Imaginary Axis');

  • 8/13/2019 MATLAB2_lezione9

    14/16

    The complex magnitude squared of Y is called thepower, and a plot of power versus frequency is a"periodogram".

    n=length(Y); power = abs(Y(1:floor(n/2))).^2; nyquist = 1/2;

    freq = (1:n/2)/(n/2)*nyquist; plot(freq,power) xlabel('cycles/year') title('Periodogram')

  • 8/13/2019 MATLAB2_lezione9

    15/16

    The scale in cycles/year is somewhat inconvenient. Wecan plot in years/cycle and estimate the length of onecycle.

    plot(freq(1:40),power(1:40)) xlabel('cycles/year')

    Now we plot power versus period for convenience(where period=1./freq). As expected, there is a veryprominent cycle with a length of about 11 years.

    period=1./freq; plot(period,power); axis([0 40 0 2e+7]); ylabel('Power'); xlabel('Period (Years/Cycle)');

  • 8/13/2019 MATLAB2_lezione9

    16/16