0% found this document useful (0 votes)
12 views8 pages

Fourierseries 1

This MATLAB code defines and plots Fourier series components and their sums to approximate a square wave signal over time. It generates the first four non-zero Fourier components, plots them individually and summed, and plots the error between the approximations and the original signal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Fourierseries 1

This MATLAB code defines and plots Fourier series components and their sums to approximate a square wave signal over time. It generates the first four non-zero Fourier components, plots them individually and summed, and plots the error between the approximations and the original signal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CODE:

%MATLAB CODE FOR FOURIER SERIES

%A. PLOTTING FIRST 4 NON ZERO FOURIER COMPONENTS

clc;
close all;
clear all;

T=2;
W=pi;
t=0:0.001:4;
sqr=[0.5*ones(1,1001) -0.5*ones(1,1000) 0.5*ones(1,1000) -0.5*ones(1,1000)];

Harmonic_1 = 2/pi*sin(W*t);
Harmonic_3 = 2/(3*pi)*sin(3*W*t);
Harmonic_5 = 2/(5*pi)*sin(5*W*t);
Harmonic_7 = 2/(7*pi)*sin(7*W*t);

figure(1)
subplot(2,2,1)
plot(t,sqr,t,Harmonic_1)
xlabel('Time')
ylabel('Amplitude')
grid
title('First non zero fourier component')
axis([ 0 4 -0.7 0.7])

subplot(2,2,2)
plot(t,sqr,t,Harmonic_3)
xlabel('Time')
ylabel('Ampiltude')
grid
title('Second non zero fourier component')
axis([ 0 4 -0.7 0.7])

subplot(2,2,3)
plot(t,sqr,t,Harmonic_5)
xlabel('Time')
ylabel('Ampiltude')
grid
title('Third non zero fourier component')
axis([ 0 4 -0.7 0.7])
subplot(2,2,4)
plot(t,sqr,t,Harmonic_7)

1
xlabel('Time')
ylabel('Ampiltude')
grid
title('Fourth non zero fourier component')

axis([ 0 4 -0.7 0.7])

%B. Plotting first 4 non zero fourier component

figure(2)
plot(t,sqr,t,Harmonic_1,t,Harmonic_3,t,Harmonic_5,t,Harmonic_7)
xlabel('Time')
ylabel('Amplitude')
grid
title('First four non zero fourier component 22071A1005')
axis([ 0 4 -0.7 0.7])
legend('Square','Harmonic-1','Harmonic-3','Harmonic-5','Harmonic-7')

2
%C. succesive approximation of sum of fse on seperate plots

Harmonic_13 = Harmonic_1+Harmonic_3;
Harmonic_135 = Harmonic_1+Harmonic_3+Harmonic_5;
Harmonic_1357 = Harmonic_1+Harmonic_3+Harmonic_5+Harmonic_7;

figure(3)
subplot(2,2,1)
plot(t,sqr,t,Harmonic_1)
xlabel('Time')
ylabel('amplitude')
grid
title('Sum of harmonics-1(22071a1005)')
axis([ 0 4 -0.7 0.7])

subplot(2,2,2)
plot(t,sqr,t,Harmonic_13)
xlabel('Time')
ylabel('amplitude')
grid
title('Sum of harmonics-1 and 3')
axis([ 0 4 -0.7 0.7])

subplot(2,2,3)
plot(t,sqr,t,Harmonic_135)

3
xlabel('Time')
ylabel('amplitude')
grid
title('Sum of harmonics-1,3 and 5')
axis([ 0 4 -0.7 0.7])

subplot(2,2,4)
plot(t,sqr,t,Harmonic_1357)
xlabel('Time')
ylabel('amplitude')
grid
title('Sum of harmonics-1,3,7 and 5')
axis([ 0 4 -0.7 0.7])

%D.Succesive Partial Approimation sums of FSE on same axis


figure(4)
plot(t,sqr,t,Harmonic_1,t,Harmonic_13,t,Harmonic_135,t,Harmonic_1357)
xlabel('Time')
ylabel('Amplitude')
grid
title('Succesive partial approximation sums 22071A1005')
axis([ 0 4 -0.7 0.7])
legend('Square','Harmonic-13','Harmonic-135','Harmonic-1357')

4
%E. plotting Errors

Error_1 = sqr-Harmonic_1;
Error_13 = sqr-Harmonic_13;
Error_135 = sqr-Harmonic_135;
Error_1357 = sqr-Harmonic_1357;

figure(5)
subplot(2,2,1)
plot(t,sqr,t,Error_1)
xlabel('Time')
ylabel('Amplitude')
grid
title('error due to the harmonic-1')
axis([ 0 4 -0.7 0.7])

subplot(2,2,2)
plot(t,sqr,t,Error_13)
xlabel('Time')
ylabel('Amplitude')
grid
title('error due to the harmonic-13')
axis([ 0 4 -0.7 0.7])

subplot(2,2,3)

5
plot(t,sqr,t,Error_135)
xlabel('Time')
ylabel('Amplitude')
grid
title('error due to the harmonic-135')
axis([ 0 4 -0.7 0.7])

subplot(2,2,4)
plot(t,sqr,t,Error_1357)
xlabel('Time')
ylabel('Amplitude')
grid
title('error due to the harmonic-1357')
axis([ 0 4 -0.7 0.7])

%F. plotting Errors using 'area' command

Error_1 = sqr-Harmonic_1;
Error_13 = sqr-Harmonic_13;
Error_135 = sqr-Harmonic_135;
Error_1357 = sqr-Harmonic_1357;

figure(6)
subplot(2,2,1)
area(t,Error_1)
xlabel('Time')

6
ylabel('Amplitude')
grid
title('Error due to the harmonic-1')
axis([ 0 4 -0.7 0.7])

subplot(2,2,2)
area(t,Error_13)
xlabel('Time')
ylabel('Amplitude')
grid
title('Error due to the harmonic-13')
axis([ 0 4 -0.7 0.7])

subplot(2,2,3)
area(t,Error_135)
xlabel('Time')
ylabel('Amplitude')
grid
title('Error due to the harmonic-135')
axis([ 0 4 -0.7 0.7])

subplot(2,2,4)
area(t,Error_1357)
xlabel('Time')
ylabel('Amplitude')
grid
title('Error due to the harmonic-1357')
axis([ 0 4 -0.7 0.7])

7
8

You might also like