# Fft Matlab

Exercise 1

Songa.wav lasts for 40 seconds and the largest value is 0.9813 and occurs at point 866149 in the array. close all;

clear all;

%This .m file read in songa, the fft of the new song is then taken, %then the song is plotted against a time axis and fft is also plotted. [x,fs,nbits]=wavread('songa');

ts=1/fs;

L=length(x);%The Length of the array and its max value are

M=max(x)%printed out in the matlab command window.

location=find(x==M)

T=1/L;

f=-L/2:1:L/2-1;

t=0:ts:(L-1)*ts;

X=fft(x);

Y=abs(fftshift(X));

YAmp=Y/10000;%The fft is devided by 10000 to give a correctly scaled y-axis

subplot(2,1,1);

plot(t,x);

xlabel('Time (s)');

ylabel('Amplitude');

subplot(2,1,2);

plot(f,YAmp,'k');

xlabel('Frequency Hz');

ylabel('Amplitude');

The below plots show songa plotted against time and the fft of it.

Ex 2

function [x,t] = ex21(f,A,theta,fs)

%This m. file is a function where the frequency, the amplitude, the phase %shift and the sampling frequency are inputs to a sin wave, this simn wave %is then has the fft taken of it and the sin wave and the fft are plotted. T=1-(1/fs);

t=0:1/fs:T;

freq=-fs/2:1:(fs-1)/2;

x=A*sin((2*pi*f*t)+theta);

X=abs(fft(x));

Y=fftshift(X);

L=length(x)/2;

YAmp=Y/L;

%The fft is devided by half the length of the array to give a %correctly scaled y-axis.

subplot(2,1,1);

plot(t,x);

ylabel('Amplitude');

xlabel('Time (s)');

subplot(2,1,2);

stem(freq,YAmp,'k');

ylabel('Amplitude');

xlabel('Frequency (Hz)');

end

The first plot below is of a sin wave with frequency 10Hz, amplitude 1 and over a time period of 1 second. The second plot below is of a fft of this sin wave clearly shows two peaks at frequency’s 10 and -10 of amplitude 1.

This function below is the modified function of the one above so that it would work nicely in Ex3 where different sin waves where added together. function [x,t] = ex2(f,A,theta,fs,T)

%This function has the inputs frequency, the amplitude, the phase %shift, the sampling frequency and the period respectively, and returns a %sin wave with those proprieties.

t=0:1/fs:T-(1/fs);

x=A*sin((2*pi*f*t)+theta);

end

Ex 2.1

function [x,t] = ex22(f,A,theta,fs)

%This m. file is a function where the frequency, the amplitude, the phase %shift and the sampling frequency are inputs to a sin wave, this simn wave %is then has the fft taken of it and the sin wave and the fft are plotted. %T is changed from 1-1/fs to 2-1/fs in this function

T=2-1/fs;

t=0:1/fs:T;

freq=-fs/2:0.5:(fs-1)/2;

x=A*sin((2*pi*f*t)+theta);

X=abs(fft(x));

Y=fftshift(X);

L=length(x)/2;

YAmp=Y/L;

%The fft is devided by half the length of the array to give a %correctly scaled y-axis.

subplot(2,1,1);

plot(t,x);

xlabel('Time (s)');

ylabel('Amplitude');

subplot(2,1,2);

stem(freq,YAmp,'k');

xlabel('Frequency Hz');

ylabel('Amplitude');

end

The plot first plot below has the same as the function had before changing T, but lasts for twice as long, as you would expect. The peaks in the fft are in the same place and to the same amplitude, as expected, because the same input values where entered into this function as the one previous.

Ex 3

close all;

clear all;

%This .m file creates 3 sin waves of different frequency and amplitude %using the function ex2, then adds them together. The fft of this %combination of the sin waves is taken, then graphs are plotted of the 3 %sin waves and there combination and also an fft centred around the %frequency 0 is plotted.

f0=10;

A=1;

theta=0;

fs=1000;

T=1;

t=0:1/fs:T-(1/fs);

f=-fs/2:1/T:fs/2-1/T;

x1=ex2(f0,A*2,theta,fs,T);%

x2=ex2(f0*2,A*3,theta,fs,T);

x3=ex2(f0*3,A*1.5,theta,fs,T);

x=x1+x2+x3;

X=fft(x);

Y=abs(fftshift(X));

L=length(x)/2;

YAmp=Y/L;

%The fft is divided by half the length of the array to give a %correctly scaled y-axis.

subplot(5,1,1);

plot(t,x1);

hold on;

ylabel('Amplitude');

xlabel('Time (s)');

subplot(5,1,2);

plot(t,x2);

hold on;

ylabel('Amplitude');

xlabel('Time...

Please join StudyMode to read the full document