from time domain to frequency domain and back to time domain
4 views (last 30 days)
Show older comments
Hi to everybody,
I am trying to jump from a frequency response to an impulse response and I need to start from sth basic so I understand how it works.
Can someone please help me understand why the last plot doesn't look like the first (the one that is commented out in the code) one? I would expect them to be the same.
% N=100000; % the number of data points
% fs=44100; % sample rate, number of samples per second
% dt=1/fs;% time increment (in seconds per sample)
% t=dt*(0:N-1); % time domain (in seconds)
% fc=100; % carrier frequency
% x=sin(2*pi*fc*t); % sine
% figure()
% plot(t,x)
%spectrum
X=fftshift(fft(x)); % DFT and shift center to zero
df=fs/N; % the frequency increment
f=-fs/2:df:fs/2-df; % create the frequency axis
figure()
plot(f,abs(X))
% reverse procedure
y=ifft(X); % inverse fft
N2=length(X); % determine the length of the signal
dt2=1/fs; % determine the time increment
tim=0:dt2:(N2-1)*dt2; %create the time axis
figure()
plot(tim,y)
0 Comments
Accepted Answer
Dr. Seis
on 16 May 2012
Your "X" has shifted the zero frequency amplitude to the center. You need to use ifftshift to get it back where Matlab expects... i.e.:
y = ifft(ifftshift(X));
3 Comments
Kacper Muszynski
on 29 Mar 2022
Thank you very much for this, was stuck on it for the longest time. However, the signal I get appears to be shifted by pi/2, any ideas?
clear all;
clc;
n = -8*pi:0.01:8*pi;
x1 = 2*sin(2*n);
subplot(3,1,1); plot(n,x1,'-b'), grid on,axis([-8*pi 8*pi -2 2]);
L = length(x1);
X = fft(x1,L);
X_amp = abs(fftshift(X));
w = linspace(-pi,pi,L);
subplot(3,1,2), plot(w/pi, X_amp), grid on;
x2 = X_amp;
x2 = ifft(ifftshift(x2)); %shift back from centre
x2 = fftshift(x2);
subplot(3,1,3), stem(n,x2),grid on,axis([-8*pi 8*pi -2 2]);
More Answers (0)
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!