fourier transform shifted graphic

2 views (last 30 days)
clear all; close all; clc;
x1=-37:0.1:-31.5;
eq1 = 0*x1+3.3;
x2=-31.5:0.1:-29.5;
eq2 = 3.3*(-29.5-x2)/(2);
x3=-29.5:0.1:-24;
eq3 = 3.3*(-29.5-x3)/(5.5);
x4=-24:0.1:-18.5;
eq4=0*x4-3.3;
x5=-18.5;
eq5 =3.3;
x6=-18.5:0.1:-13;
eq6 = 0*x6+3.3;
x7=-13:0.1:-11;
eq7 = 3.3*(-11-x7)/(2);
x8=-11:0.1:-5.5;
eq8 = 3.3*(-11-x8)/(5.5);
x9=-5.5:0.1:0;
eq9 = 0*x9-3.3;
x10=0;
eq10=3.3;
x11=0:0.1:5.5;
eq11 = (0*x11)+3.3;
x12=5.5:0.1:7.5;
eq12 = 3.3*(7.5-x12)/2;
x13=7.5:0.1:13;
eq13=3.3*(7.5-x13)/5.5;
x14=13:0.1:18.5;
eq14=(0*x14)-3.3;
x15=18.5;
eq15 = 3.3;
x = [x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 ];
y = [eq1 eq2 eq3 eq4 eq5 eq6 eq7 eq8 eq9 eq10 eq11 eq12 eq13 eq14 eq15 ];
plot(x, y, 'r')
grid on; hold on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms t
A=3.3;
t1=(5+6)/2;
t2=t1+2;
t3=t1+t2;
t4=t1+t3;
w=(2*pi)/t4;
sum=0;
a0=(2/t4)*(int(t*0+A,0,t1)+int(A*(7.5-t)/(2),t1,t2)+int(A*(7.5-t)/(5.5),t2,t3)+int(t*0-A,t3,t4))
for n=1:20
an=(2/t4)*(int((t*0+A)*cos((n*w)*t),0,t1)+int(A*(7.5-t)/(2)*cos((n*w)*t),t1,t2)+int(A*(7.5-t)/(5.5)*cos((n*w)*t),t2,t3)+int((t*0-A)*cos((n*w)*t),t3,t4))
bn=(2/t4)*(int((t*0+A)*sin((n*w)*t),0,t1)+int(A*(7.5-t)/(2)*sin((n*w)*t),t1,t2)+int(A*(7.5-t)/(5.5)*sin((n*w)*t),t2,t3)+int((t*0-A)*sin((n*w)*t),t3,t4))
sum=sum+((an*cos((n*w)*t)+bn*sin((n*w)*t)+(a0/2)))
end
grid on;hold on;
ezplot(t,(sum),[-2*t4,t4]);
I haven't been able to find out for hours why this graph is shifted. Maybe someone can help and see the error? Thanks.

Accepted Answer

David Goodmanson
David Goodmanson on 10 Dec 2021
Edited: David Goodmanson on 10 Dec 2021
Hello MK
First of all it is not a good idea to have a variable called 'sum' because that's the name of a basic Matlab function. Matlab is case sensitive so when I ran the code I changed the variable to 'Sum' (easy because when you change the name of a variable in the editor, Matlab offers up an optional autochange of every other instance of the variable).
The problem happens because you have a0/2 inside the for loop, so that a0/2 gets added in 20 times. If you remove a0/2 from the loop and add Sum = Sum + a0/2 past the end of the loop, it works.
  2 Comments
David Goodmanson
David Goodmanson on 10 Dec 2021
Hi yok,
We have all been there, staring at the same set of expressions for hours and hours, having some sort of mental block. Then someone else comes along and says, what about <whatever> and then we say, aak! how could I have been such an idiot? It happens. One of the useful things about this site is having several sets of eyes take a look..

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by