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:
Help with Fourier reconstruction

Subject: Help with Fourier reconstruction

From: Conrad

Date: 22 Nov, 2011 03:11:09

Message: 1 of 3

Hi,
I have the code below to reconstruct a signal. I'm trying to regenerate y2 from y, so that for each value of x2, there is a corresponding y2 value. For N2=15, the signal appears to be shifted compared to the original. Can anyone tell me how to fix this problem? Thank you

x=[1;2;4;5;6;7;8;10;12;14;15];
y=[8.46;8.64;8.69;8.69;8.57;8.64;8.46;8.39;8.39;8.93;9.3];
ff2=fft(y);
N = length(y);
N2=x(end); % length of reconstruction
y2= ff2(1)/(N);
p = 2*pi*((0:N2-1)/N2);
for k = 1:(length(ff2)/2)
    a = ff2(k+1);
    phi = k*p;
    if k < N2/2; s = 2; else s = 1; end
    component = s*(real(a)*cos(phi) - imag(a)*sin(phi))/N;
    y2 = y2 + component;
end
x2=1:N2;
figure; plot(x,y); hold on; plot(x2,y2,'r');

Subject: Help with Fourier reconstruction

From: Greg Heath

Date: 22 Nov, 2011 20:32:08

Message: 2 of 3

"Conrad" wrote in message <jaf3sd$ace$1@newscl01ah.mathworks.com>...
> Hi,
> I have the code below to reconstruct a signal. I'm trying to regenerate y2 from y, so that for each value of x2, there is a corresponding y2 value. For N2=15, the signal appears to be shifted compared to the original. Can anyone tell me how to fix this problem? Thank you
>
> x=[1;2;4;5;6;7;8;10;12;14;15];
> y=[8.46;8.64;8.69;8.69;8.57;8.64;8.46;8.39;8.39;8.93;9.3];

Problem #1 : x does not start at x=0 which is assumed by ifft. This cause the shift.

> ff2=fft(y);

Problem #2: fft is only valid when diff(x) is constant. For nonuniform spacing you need to use dft instead of fft. Search for heath dftgh6 and modify to suit your needs.

> N = length(y);
> N2=x(end); % length of reconstruction
> y2= ff2(1)/(N);
> p = 2*pi*((0:N2-1)/N2);
> for k = 1:(length(ff2)/2)
> a = ff2(k+1);
> phi = k*p;
> if k < N2/2; s = 2; else s = 1; end
> component = s*(real(a)*cos(phi) - imag(a)*sin(phi))/N;
> y2 = y2 + component;
> end
> x2=1:N2;
> figure; plot(x,y); hold on; plot(x2,y2,'r');

Hope this helps.

Greg

Subject: Help with Fourier reconstruction

From: Vilnis Liepins

Date: 28 Nov, 2011 07:27:08

Message: 3 of 3

"Conrad" wrote in message <jaf3sd$ace$1@newscl01ah.mathworks.com>...
> Hi,
> I have the code below to reconstruct a signal. I'm trying to regenerate y2 from y, so that for each value of x2, there is a corresponding y2 value. For N2=15, the signal appears to be shifted compared to the original. Can anyone tell me how to fix this problem? Thank you
>
> x=[1;2;4;5;6;7;8;10;12;14;15];
> y=[8.46;8.64;8.69;8.69;8.57;8.64;8.46;8.39;8.39;8.93;9.3];
> ff2=fft(y);
> N = length(y);
> N2=x(end); % length of reconstruction
> y2= ff2(1)/(N);
> p = 2*pi*((0:N2-1)/N2);
> for k = 1:(length(ff2)/2)
> a = ff2(k+1);
> phi = k*p;
> if k < N2/2; s = 2; else s = 1; end
> component = s*(real(a)*cos(phi) - imag(a)*sin(phi))/N;
> y2 = y2 + component;
> end
> x2=1:N2;
> figure; plot(x,y); hold on; plot(x2,y2,'r');

You can use Extended DFT available on fileexchange http://www.mathworks.com/matlabcentral/fileexchange/11020-extended-dft
Application of program edft.m is following: put NaNs (Not-a-Number) where data are missing
y_nan=[8.46;8.64;NaN;8.69;8.69;8.57;8.64;8.46;NaN;8.39;NaN;8.39;NaN;8.93;9.3];
and run
y2=real(ifft(edft(y_nan,124)));
The first 15 samples of y2 are your data where NaNs are replaced with interpolated values, the rest of y2 are forward/backward extrapolation.

Regards
Vilnis

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