Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Calculate sinus and cosinus amplitude (Fourier coefficients)

Subject: Calculate sinus and cosinus amplitude (Fourier coefficients)

From: Hermano Cappa

Date: 2 Feb, 2012 13:12:11

Message: 1 of 5

Hi,

I'm trying to calculate the Fourier coefficients of a signal via the command FFT. The real part is the cosinus amplitude and the imaginary part is the sinus amplitude. However, if I plot the signal again (in time domain), using the Fourier coefficients I have calculated, I get a signal wish is shifted in time. Why do I not get the same signal back?

t=0:0.01:1;
data = 2*sin(2*pi*4*t)+2*cos(2*pi*4*t);
N = length(t);
data_fft = 2/N*fft(data);
figure
plot(t,data)

A_cos=real(data_fft);
A_sin=imag(data_fft);

n = 2000;
theta = (0:1:n-1)'*2*pi/n;
signal = 0;

for k=1:10
dummy = A_cos(k+1)*cos(k*theta) + A_sin(k+1)*sin(k*theta);
signal = signal + dummy;
end

figure
plot(signal)

Subject: Calculate sinus and cosinus amplitude (Fourier coefficients)

From: pietro

Date: 2 Feb, 2012 13:55:10

Message: 2 of 5

"Hermano Cappa" wrote in message <jge23b$pas$1@newscl01ah.mathworks.com>...
> Hi,
>
> I'm trying to calculate the Fourier coefficients of a signal via the command FFT. The real part is the cosinus amplitude and the imaginary part is the sinus amplitude. However, if I plot the signal again (in time domain), using the Fourier coefficients I have calculated, I get a signal wish is shifted in time. Why do I not get the same signal back?
>
> t=0:0.01:1;
> data = 2*sin(2*pi*4*t)+2*cos(2*pi*4*t);
> N = length(t);
> data_fft = 2/N*fft(data);
> figure
> plot(t,data)
>
> A_cos=real(data_fft);
> A_sin=imag(data_fft);
>
> n = 2000;
> theta = (0:1:n-1)'*2*pi/n;
> signal = 0;
>
> for k=1:10
> dummy = A_cos(k+1)*cos(k*theta) + A_sin(k+1)*sin(k*theta);
> signal = signal + dummy;
> end
>
> figure
> plot(signal)

Hi,

have you tried in this way:

t=0:0.01:1;
data = 2*sin(2*pi*4*t)+2*cos(2*pi*4*t);
N = length(t);
data_fft = 2/N*fft(data);
data_fft=fftshift(data_fft);
figure
plot(t,data)

Subject: Calculate sinus and cosinus amplitude (Fourier coefficients)

From: Hermano Cappa

Date: 2 Feb, 2012 14:36:10

Message: 3 of 5

"pietro " <bracardi82@email.it> wrote in message <jge4ju$417$1@newscl01ah.mathworks.com>...
> "Hermano Cappa" wrote in message <jge23b$pas$1@newscl01ah.mathworks.com>...
> > Hi,
> >
> > I'm trying to calculate the Fourier coefficients of a signal via the command FFT. The real part is the cosinus amplitude and the imaginary part is the sinus amplitude. However, if I plot the signal again (in time domain), using the Fourier coefficients I have calculated, I get a signal wish is shifted in time. Why do I not get the same signal back?
> >
> > t=0:0.01:1;
> > data = 2*sin(2*pi*4*t)+2*cos(2*pi*4*t);
> > N = length(t);
> > data_fft = 2/N*fft(data);
> > figure
> > plot(t,data)
> >
> > A_cos=real(data_fft);
> > A_sin=imag(data_fft);
> >
> > n = 2000;
> > theta = (0:1:n-1)'*2*pi/n;
> > signal = 0;
> >
> > for k=1:10
> > dummy = A_cos(k+1)*cos(k*theta) + A_sin(k+1)*sin(k*theta);
> > signal = signal + dummy;
> > end
> >
> > figure
> > plot(signal)
>
> Hi,
>
> have you tried in this way:
>
> t=0:0.01:1;
> data = 2*sin(2*pi*4*t)+2*cos(2*pi*4*t);
> N = length(t);
> data_fft = 2/N*fft(data);
> data_fft=fftshift(data_fft);
> figure
> plot(t,data)

Pietro,

fftshift is used when you want to move the DC-component to the middle of the spectrum, this doesn't solve my problem unfortunately!!

Subject: Calculate sinus and cosinus amplitude (Fourier coefficients)

From: Hermano Cappa

Date: 2 Feb, 2012 17:23:09

Message: 4 of 5

"Hermano Cappa" wrote in message <jge70q$ci1$1@newscl01ah.mathworks.com>...
> "pietro " <bracardi82@email.it> wrote in message <jge4ju$417$1@newscl01ah.mathworks.com>...
> > "Hermano Cappa" wrote in message <jge23b$pas$1@newscl01ah.mathworks.com>...
> > > Hi,
> > >
> > > I'm trying to calculate the Fourier coefficients of a signal via the command FFT. The real part is the cosinus amplitude and the imaginary part is the sinus amplitude. However, if I plot the signal again (in time domain), using the Fourier coefficients I have calculated, I get a signal wish is shifted in time. Why do I not get the same signal back?
> > >
> > > t=0:0.01:1;
> > > data = 2*sin(2*pi*4*t)+2*cos(2*pi*4*t);
> > > N = length(t);
> > > data_fft = 2/N*fft(data);
> > > figure
> > > plot(t,data)
> > >
> > > A_cos=real(data_fft);
> > > A_sin=imag(data_fft);
> > >
> > > n = 2000;
> > > theta = (0:1:n-1)'*2*pi/n;
> > > signal = 0;
> > >
> > > for k=1:10
> > > dummy = A_cos(k+1)*cos(k*theta) + A_sin(k+1)*sin(k*theta);
> > > signal = signal + dummy;
> > > end
> > >
> > > figure
> > > plot(signal)
> >
> > Hi,
> >
> > have you tried in this way:
> >
> > t=0:0.01:1;
> > data = 2*sin(2*pi*4*t)+2*cos(2*pi*4*t);
> > N = length(t);
> > data_fft = 2/N*fft(data);
> > data_fft=fftshift(data_fft);
> > figure
> > plot(t,data)
>
> Pietro,
>
> fftshift is used when you want to move the DC-component to the middle of the spectrum, this doesn't solve my problem unfortunately!!


If I change A_sin = imag(data_fft) to A_sin = -imag(data_fft) my problem is solved. Why is this?

Subject: Calculate sinus and cosinus amplitude (Fourier coefficients)

From: Matt J

Date: 2 Feb, 2012 19:35:12

Message: 5 of 5

"Hermano Cappa" wrote in message <jgegpt$hct$1@newscl01ah.mathworks.com>...
>
>
> If I change A_sin = imag(data_fft) to A_sin = -imag(data_fft) my problem is solved. Why is this?
============

Probably because of the identity

A*sin(z)=j*(-A/2)exp(j*z) + j * (A/2)exp(-j*z)

Notice how complex sinusoids with positive frequencies get NEGATED
coefficients -A/2 .

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us