Vous êtes sur la page 1sur 17

MODULE 5- MATLAB FOR SINE

WAVES PART II

SUM OF SINE WAVES OF DIFFERENT


FREQUENCIES
t=0:0.01:20;
a=sin(t);
subplot(3,1,1)

% Creates an array t
% Generates a sine wave with period for the elements of t
% Breaks the figure window into an 3X1 matrix of plots and
% chooses the first plot row wise
plot(t,a,'LineWidth',2) % Generates a plot of t vs a
grid on
% Displays a grid on the plot
axis([0 20 -1.2 1.2])
xlabel('time')
% Labels the x axis
ylabel('sin(t)')
% Labels the y axis
title('Plot to show that the sum of sine waves of different frequencies does not
result in a sine wave')
% Displays a title for the plot
% Code continued on next page

b= sin(4*t);
subplot(3,1,2)
plot(t,b,'LineWidth',2)
grid on
axis([0 20 -1.2 1.2])
xlabel('time')
ylabel('sin(4t)')

% Generates a plot of t vs c
% Displays a grid on the plot
% Labels the x axis
% Labels the y axis

c= a + b;
subplot(3,1,3)
plot(t,c,'LineWidth',2) % Generates a plot of t vs c
grid on
% Displays a grid on the plot
axis([0 20 -2.4 2.4])
xlabel('time')
% Labels the x axis
ylabel('sin(t)+sin(4t)') % Labels the y axis

Plot to show that the sum of sine waves of different frequencies does not result in a sine wave
sin(t)

1
0
-1
0

10
time

12

14

16

18

20

10
time

12

14

16

18

20

10
time

12

14

16

18

20

sin(4t)

1
0

sin(t)+sin(4t)

-1

2
0
-2

GENERATION OF A SQUARE WAVE FROM


SINE WAVES OF DIFFERENT FREQUENCIES
% Generation of square wave from sine waves of different frequencies
t=0:0.01:20;
y1 = sin(t);
plot(t,y1,'g','LineWidth',2)
grid on
xlabel('time')
% Labels the x axis
title('Generation of a square wave from sine waves') % Displays a title for the plot
hold on
y2 = sin(t) + sin(3*t)/3 + sin(5*t)/5 + sin(7*t)/7 + sin(9*t)/9;
plot(t,y2,'k','LineWidth',2)

Generation of a square wave from sine waves


1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10
time

12

14

16

18

20

GENERATION OF A SAWTOOTH WAVE FROM


SINE WAVES OF DIFFERENT FREQUENCIES
clc;
clear;
close all;
t=0:0.001:10;
N= length(t);

% Creates an array t

s = zeros(1,N);
for k=1:1:1001
s= -(sin(2*pi*k.*t)./k)+ s;
end
plot(t,s,'LineWidth',2)
grid on
title('Generation of Sawtooth waveform from Sine Waves of different frequencies')

Generation of Sawtooth waveform from Sine Waves of different frequencies


2
1.5
1
0.5
0
-0.5
-1
-1.5
-2

10

FFT IN MATLAB
The Fourier theorem states that any waveform can be duplicated
by the superposition of a series of sine and cosine waves.
We did this while generating a square from the superposition of
sine waves of different frequencies!
We use Fourier transform to perform the decomposition
Were going from time domain to frequency domain
FFT( Fast Fourier transform) is an algorithm to find the Fourier
transform of an input sequence
We use the MATLAB command fft
>> g=[1 2 3 4 5];
>> fft(g)
ans =

15.0000
-2.5000 + 3.4410i -2.5000 + 0.8123i -2.5000 0.8123i -2.5000 - 3.4410i

SPECTRUM OF A PERIODIC(SINE) WAVE


clc;
clear;
close all;
N=2^13;
% Number of samples
fmax=64;
% Maximum frequency in Hz
fs=2*fmax;
% Sampling Frequency
df=fs/N;
% frequency spacing
dt=1./(df.*N); % time spacing
t=(1:N)*dt-dt; % time samples
f(1:N/2)=(1:N/2)*df-df;
% Nonnegative frequency samples
f(N/2+1:N)=-fmax+(0:N/2-1)*df; % Negative frequency samples
f2=fftshift(f);
% shifting the values in f so that it goes from f max to +fmax
% Code continued on next page

xt = sin(2*pi*10*t); % sine wave of frequency 4 in time domain


xf = fft(x).*dt;
% sine wave in the frequency domain
xf = 20*log10(fftshift(abs(xf))); % converting to dB, taking the absolute value
% and shifting the values to correspond to the
% frequencies in f
plot(f2,xf,'LineWidth',2)
grid on
title('Spectrum of a Sine wave of one frequency')
xlabel('Frequency in Hz')
ylabel('Magnitude in dB')

Spectrum of a Sine wave of one frequency


50

-50

Magnitude in dB

-100

-150

-200

-250

-300

-350
-80

-60

-40

-20

0
Frequency in Hz

20

40

60

80

SPECTRUM OF SPEECH
Type this in the MATLAB command window
>>demoai_fft

CHIRP SIGNAL

A chirp is a signal in which the frequency increases ('up-chirp') or decreases


('down-chirp') with time.
Code to generate and play a chirp signal :

clc;
clear
close all;

Fs = 4000;
T = 1;
dt = 1/Fs;
N = T*Fs;
% Number of samples
df = Fs/N;
% frequency spacing
t = (1:N)*dt - dt;
% time samples
f(1:N/2)=(1:N/2)*df-df;
% Nonnegative frequency samples
f(N/2+1:N)=-fmax+(0:N/2-1)*df; % Negative frequency samples
f2=fftshift(f);
% shifting the values in f so that it goes from f max to +fmax
% Code continued on next page

x = cos(f.*t);
wavplay(x,1/dt)
plot(t,x)
axis([0 2.2 -1.2 1.2])
xf = fft(x).*dt;
xf = fftshift(abs(xf)); % Absolute value of the Fourier coefficients
figure
stem(f,xf)
grid on
title(Spectrum of chirp signal)

GUESS WHAT SIGNAL IS FORMED


WHEN YOU RUN THIS CODE
clc;
clear;
close all;
dt = 0.001;
t=0:dt:4-dt;
N= length(t);

% Creates an array t

x = zeros(1,N);
x(1,N/4:N/2) = 1:-4/N:0;
x(1,N/2:(3*N/4)) = 0:4/N:1;

% Find the Fourier Series Co-efficients:


X = zeros(1,N);
X = X + x(1);
k=0:1:(N-1);
for n = 1:N-1
X = X + (x(n+1)*exp(-j*2*pi*k*n/N));
end
%Reconstruction Using Fourier Co-efficients
x_r = zeros(1,N);
x_r = x_r + X(1);
n = 0:N-1;
for k=1:10 % Change the value of k to change the number of coefficients used in reconstruction
x_r = (x_r + (X(k+1)*exp(j*2*pi*k*n/N)));
end

%plot(x_r)
plot(t,x_r,'LineWidth',2)
grid on

Vous aimerez peut-être aussi