From: Saurabh on
Hello

I have a rectangular function defined as follows
tstop = 10E-3;
t=-0.1:dt:0.1;
p_i=(1/2)*(sign(t)-sign(t-tstop));
input = figure;
plot(t,p_i)
rge = 0.05;
axis([-rge rge 0 1]);
xlabel('time')
ylabel('amplitude')
title('Input step function')

Then I take a Centered FFT

[pw,frequencyRange] = centeredFFT(p_i,1/dt);
positiveFFT = figure;
stem(frequencyRange,abs(pw));
% set(positiveFFT,'Position',[500,500,500,300])
xlabel('Freq (Hz)')
ylabel('Amplitude')
title('Using the positiveFFT function')
grid
axis([0 500 0 0.05]);
xlabel('frequency')
ylabel('amplitude')
title('FFT of the input step function')

where the function centeredFFT is

function [X,freq]=centeredFFT(x,Fs)
%this is a custom function that helps in plotting the two-sided spectrum
%x is the signal that is to be transformed
%Fs is the sampling rate

N=length(x);

%this part of the code generates that frequency axis
if mod(N,2)==0
k=-N/2:N/2-1; % N even
else
k=-(N-1)/2:(N-1)/2; % N odd
end
T=N/Fs;
freq=k/T; %the frequency axis

%takes the fft of the signal, and adjusts the amplitude accordingly
X=fft(x)/N; % normalize the data
X=fftshift(X); %shifts the fft data so that it is centered

Now I want to take IFFT of the function but i do not get back the same input signal

orignal_signal = ifft(YfreqDomain);
time = ifft(frequencyRange);
reterived = figure
plot( time,orignal_signal)
in

Could any one guide in simple terms where I am wrong ?

I also tried this

orignal_signal = ifft(ifftshift(YfreqDomain));
time = ifft(ifftshift(frequencyRange));
reterived = figure
plot( time,orignal_signal)

but could not reterive back original signal
From: Rune Allnor on
On 18 Mar, 11:37, "Saurabh " <saura...(a)student.ethz.ch> wrote:

> but could not reterive back original signal

Try this:

N = 100;
x = randn(N,1);
X = fft(x);
y = real(ifft(X));
d = max(abs(y-x))

d should now contain a very small number (on the order
of 1e-15), which means that y == x, except for numerical
round-off.

Rune
From: Wayne King on
"Saurabh " <saurabhs(a)student.ethz.ch> wrote in message <hnsvof$2f0$1(a)fred.mathworks.com>...
> Hello
>
> I have a rectangular function defined as follows
> tstop = 10E-3;
> t=-0.1:dt:0.1;
> p_i=(1/2)*(sign(t)-sign(t-tstop));
> input = figure;
> plot(t,p_i)
> rge = 0.05;
> axis([-rge rge 0 1]);
> xlabel('time')
> ylabel('amplitude')
> title('Input step function')
>
> Then I take a Centered FFT
>
> [pw,frequencyRange] = centeredFFT(p_i,1/dt);
> positiveFFT = figure;
> stem(frequencyRange,abs(pw));
> % set(positiveFFT,'Position',[500,500,500,300])
> xlabel('Freq (Hz)')
> ylabel('Amplitude')
> title('Using the positiveFFT function')
> grid
> axis([0 500 0 0.05]);
> xlabel('frequency')
> ylabel('amplitude')
> title('FFT of the input step function')
>
> where the function centeredFFT is
>
> function [X,freq]=centeredFFT(x,Fs)
> %this is a custom function that helps in plotting the two-sided spectrum
> %x is the signal that is to be transformed
> %Fs is the sampling rate
>
> N=length(x);
>
> %this part of the code generates that frequency axis
> if mod(N,2)==0
> k=-N/2:N/2-1; % N even
> else
> k=-(N-1)/2:(N-1)/2; % N odd
> end
> T=N/Fs;
> freq=k/T; %the frequency axis
>
> %takes the fft of the signal, and adjusts the amplitude accordingly
> X=fft(x)/N; % normalize the data
> X=fftshift(X); %shifts the fft data so that it is centered
>
> Now I want to take IFFT of the function but i do not get back the same input signal
>
> orignal_signal = ifft(YfreqDomain);
> time = ifft(frequencyRange);
> reterived = figure
> plot( time,orignal_signal)
> in
>
> Could any one guide in simple terms where I am wrong ?
>
> I also tried this
>
> orignal_signal = ifft(ifftshift(YfreqDomain));
> time = ifft(ifftshift(frequencyRange));
> reterived = figure
> plot( time,orignal_signal)
>
> but could not reterive back original signal

Hi, if you want the signal back, why not just

% you never give us dt by the way
tstop = 10E-3;
dt=1e-3;
t=-0.1:dt:0.1;
p_i=(1/2)*(sign(t)-sign(t-tstop));
Pdft = fft(p_i);
p_m=ifft(Pdft,'symmetric');
norm(p_m-p_i)

Wayne
From: Saurabh on
well sorry for missing dt which is 1E-6..... I am new to matlab

but the thing is that if I just use the code

Pdft = fft(p_i);
plot (abs(Pdft))
or
Pdft = fft(p_i);
stem(abs(Pdft))

I do not get what is expected so I have not used your remaining code since Fourier transform is not right . Because my input signal is a box card function and mathematically i know what its output is going to be that is a sinc function and the way i am using centeredFFT i do get a sync function .

I need to use FFT and IFFT in my program so that is why i am looking to how to use them correctly hence making a simple test program that i put up where I have a input boxcar and by taking its Fourier Transform I do get a sync function now to see how ifft works i should get back box car function . But i do not .

the suggestion which you gave does not convince me since Pdft is not a sync function and x axis give me no information about the frequency .

Could you elaborate your answer ?

and suggest me possibly How could I recover back Box car after doing ifft with the sync function

Regards



"Wayne King" <wmkingty(a)gmail.com> wrote in message <hnt0ob$gvm$1(a)fred.mathworks.com>...
> "Saurabh " <saurabhs(a)student.ethz.ch> wrote in message <hnsvof$2f0$1(a)fred.mathworks.com>...
> > Hello
> >
> > I have a rectangular function defined as follows
> > tstop = 10E-3;
> > t=-0.1:dt:0.1;
> > p_i=(1/2)*(sign(t)-sign(t-tstop));
> > input = figure;
> > plot(t,p_i)
> > rge = 0.05;
> > axis([-rge rge 0 1]);
> > xlabel('time')
> > ylabel('amplitude')
> > title('Input step function')
> >
> > Then I take a Centered FFT
> >
> > [pw,frequencyRange] = centeredFFT(p_i,1/dt);
> > positiveFFT = figure;
> > stem(frequencyRange,abs(pw));
> > % set(positiveFFT,'Position',[500,500,500,300])
> > xlabel('Freq (Hz)')
> > ylabel('Amplitude')
> > title('Using the positiveFFT function')
> > grid
> > axis([0 500 0 0.05]);
> > xlabel('frequency')
> > ylabel('amplitude')
> > title('FFT of the input step function')
> >
> > where the function centeredFFT is
> >
> > function [X,freq]=centeredFFT(x,Fs)
> > %this is a custom function that helps in plotting the two-sided spectrum
> > %x is the signal that is to be transformed
> > %Fs is the sampling rate
> >
> > N=length(x);
> >
> > %this part of the code generates that frequency axis
> > if mod(N,2)==0
> > k=-N/2:N/2-1; % N even
> > else
> > k=-(N-1)/2:(N-1)/2; % N odd
> > end
> > T=N/Fs;
> > freq=k/T; %the frequency axis
> >
> > %takes the fft of the signal, and adjusts the amplitude accordingly
> > X=fft(x)/N; % normalize the data
> > X=fftshift(X); %shifts the fft data so that it is centered
> >
> > Now I want to take IFFT of the function but i do not get back the same input signal
> >
> > orignal_signal = ifft(YfreqDomain);
> > time = ifft(frequencyRange);
> > reterived = figure
> > plot( time,orignal_signal)
> > in
> >
> > Could any one guide in simple terms where I am wrong ?
> >
> > I also tried this
> >
> > orignal_signal = ifft(ifftshift(YfreqDomain));
> > time = ifft(ifftshift(frequencyRange));
> > reterived = figure
> > plot( time,orignal_signal)
> >
> > but could not reterive back original signal
>
> Hi, if you want the signal back, why not just
>
> % you never give us dt by the way
> tstop = 10E-3;
> dt=1e-3;
> t=-0.1:dt:0.1;
> p_i=(1/2)*(sign(t)-sign(t-tstop));
> Pdft = fft(p_i);
> p_m=ifft(Pdft,'symmetric');
> norm(p_m-p_i)
>
> Wayne
From: Matt J on
A few comments about things that could be contributing to the problem:



> %takes the fft of the signal, and adjusts the amplitude accordingly
> X=fft(x)/N; % normalize the data
===============

This doesn't seem like an appropriate normalization. If you'e trying to approximate the continuous Fourier transform, the appropriate normalization would be

X=fft(x)*Fs; %Equation 1

Also, if you invert Equation 1, you get

x=ifft(X)/Fs; %Equation 2

which shows that later, when you take the ifft, you have to undo the normalization you did in Equation 1 to get the original signal back again.



> X=fftshift(X); %shifts the fft data so that it is centered
>
> Now I want to take IFFT of the function but i do not get back the same input signal
>
> orignal_signal = ifft(YfreqDomain);
> time = ifft(frequencyRange);
=================

I'm baffled as to why you think you can or need to use the IFFT to generate the time axis. You already created a time axis, t, early in the code.