Showing posts with label fft matlab. Show all posts
Showing posts with label fft matlab. Show all posts

## fft matlab Function And ftt code with Example

Fast Fourier transform of fft function in matlab.

## Syntax:-

```Y = fft(X)
Y = fft(X,n)
Y = fft(X,n,dim)```

## Discprition for fft matlab Function

If x is an vector then it will return the Fourier transform of that vector

Let x is an matrix then fft(X) assume the colum as a vactory and return Fourier transform of each column.

Now let assume X is an multidimensional array then fft(X) treats the values of first array dimension only if the size of that array is not equal to 1 as vector and gives the Fourier transform of each vector

# Cases with the fft() MATLAB Function

Y = fft(X,n) returns the n-point DFT.

## Case 1

X is an vector and the length of X is lower then n. X will be padded with trailing zeros to length n

## Case 2

X is an vector and the length of X is Higher then n. X will be truncated to length n

## Case 2

X is an matrix then each column of that matrix treated as in the vector case

## Case 2

multidimensional array X is treated as like fft(X) the "first array dimension only if the size of that array is not equal to 1" treated as in the vector case

Y = fft(X,n,dim) gives the Fourier transform along with the dimension dim.

## Example Code For ftt() function in MATLAB

Use the help of Fourier transforms to find the frequency components of a signal buried in noise.

```Fs = 1000;            % Sampling frequency
T = 1/Fs;             % Sampling period
L = 1500;             % Length of signal
t = (0:L-1)*T;        % Time vector
Form a signal containing a 50 Hz sinusoid of amplitude 0.7 and a 120 Hz sinusoid of amplitude 1.

S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);

Corrupt the signal with zero-mean white noise with a variance of 4.

X = S + 2*randn(size(t));

Plot the noisy signal in the time domain. It is difficult to identify the frequency components by looking at the signal X(t).

plot(1000*t(1:50),X(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('t (milliseconds)')
ylabel('X(t)')

Compute the Fourier transform of the signal.

Y = fft(X);

Compute the two-sided spectrum P2. Then compute the single-sided spectrum P1 based on P2 and the even-valued signal length L.

P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);

Define the frequency domain f and plot the single-sided amplitude spectrum P1. The amplitudes are not exactly at 0.7 and 1, as expected, because of the added noise. On average, longer signals produce better frequency approximations.

f = Fs*(0:(L/2))/L;
plot(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
Now, take the Fourier transform of the original, uncorrupted signal and retrieve the exact amplitudes, 0.7 and 1.0.

Y = fft(S);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);

plot(f,P1)
title('Single-Sided Amplitude Spectrum of S(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
```

## OUTPUT For Example of fft () matlab:-

 fft () matlab output

 fftshift matlab

 fourier transform matlab