day1module05
-
Upload
chhon-channak -
Category
Documents
-
view
221 -
download
0
Transcript of day1module05
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 1/17
MODULE 5- MATLAB FOR SINE
WAVES PART II
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 2/17
SUM OF SINE WAVES OF DIFFERENT
FREQUENCIES
t=0:0.01:20; % Creates an array ta=sin(t); % Generates a sine wave with period for the elements of t
subplot(3,1,1) % 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 plotaxis([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
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 3/17
b= sin(4*t);
subplot(3,1,2)
plot(t,b,'LineWidth',2) % Generates a plot of t vs c
grid on % Displays a grid on the plot
axis([0 20 -1.2 1.2])
xlabel('time') % Labels the x axis
ylabel('sin(4t)') % 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 plotaxis([0 20 -2.4 2.4])
xlabel('time') % Labels the x axis
ylabel('sin(t)+sin(4t)') % Labels the y axis
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 4/17
0 2 4 6 8 10 12 14 16 18 20
-1
0
1
time
s i n ( t )
Plot to show that the sum of sine waves of different frequencies does not result in a sine wave
0 2 4 6 8 10 12 14 16 18 20
-1
0
1
time
s i n ( 4 t )
0 2 4 6 8 10 12 14 16 18 20
-2
0
2
time
s i n ( t ) + s i n ( 4 t )
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 5/17
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)
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 6/17
0 2 4 6 8 10 12 14 16 18 20-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
time
Generation of a square wave from sine waves
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 7/17
GENERATION OF A SAWTOOTH WAVE FROM
SINE WAVES OF DIFFERENT FREQUENCIES
clc;
clear;
close all;
t=0:0.001:10; % Creates an array t
N= length(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')
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 8/17
0 1 2 3 4 5 6 7 8 9 10-2
-1.5
-1
-0.5
0
0.5
1
1.5
2Generation of Sawtooth waveform from Sine Waves of different frequencies
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 9/17
FFT IN MATLAB• The Fourier theorem states that any waveform can be duplicatedby 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
• We’re going from time domain to frequency domain
• FFT( Fast Fourier transform) is an algorithm to find the Fouriertransform 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
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 10/17
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 samplesf(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
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 11/17
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')
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 12/17
-80 -60 -40 -20 0 20 40 60 80-350
-300
-250
-200
-150
-100
-50
0
50Spectrum of a Sine wave of one frequency
Frequency in Hz
M a g n i t u d e
i n d B
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 13/17
SPECTRUM OF SPEECH
• Type this in the MATLAB command window
>>demoai_fft
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 14/17
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 spacingt = (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
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 15/17
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 coefficientsfigure
stem(f,xf)
grid on
title(‘Spectrum of chirp signal’)
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 16/17
GUESS WHAT SIGNAL IS FORMED
WHEN YOU RUN THIS CODE
clc;
clear;
close all;
dt = 0.001;
t=0:dt:4-dt; % Creates an array t
N= length(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;
8/9/2019 day1module05
http://slidepdf.com/reader/full/day1module05 17/17
% 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