# Magnitude and Phase Spectra to Impulse Signal by Inverse Fourier Transform

We have programmed a MATLAB function to calculate Fourier Series Coefficients from a time-series signal in https://www.smart-coding.net/fourier-series-and-rao/. In this post, we will present a function to transform power spectrum data to time-series signals (impulse response).

``````function [y,t]=Freq2Time(w,H)
%This function calculate the time signal using ifft function for the
%signal's spectrum
% w: frequency with constant difference
% H: vector with complex elements,

dw=w(2)-w(1);
if(w(1)>0)
[m,n]=size(w);
[p,q]=size(H);
if(m>1)
if(p>1)
H=[zeros(NH,1);H];
else
H=[zeros(1,NH) H];
end
else

if(p>1)
H=[H(1)*ones(NH,1);H];
else
H=[H(1)*ones(1,NH) H]';
end
end

end

Ts=(2*pi/max(w));
N=length(w);
t=0:Ts:(N-1)*Ts;

% compute ifft
IR_data=real((ifft(H, 'symmetric' )/Ts));
y=IR_data(1:N);
if(y(1)>0)
y(1)=2*y(1);
end
end
``````

An example using the above function in comparison with real result is given as follows:

``````sys=tf([1,2,300],[1,2,3,1]);
w=0:0.01:62.83;
N=length(w);
[mag,phase]=bode(sys,w);

H=mag(:).*exp(1j*phase(:)*pi/180);

% setup time step for ifft
Ts=(2*pi/max(w));
t=0:Ts:(N-1)*Ts;
y2 = Freq2Time(w,H);
[y1,t1]=impulse(sys,t);
figure(1);
plot(t,y2,'r',t1,y1,'b--');
xlim([0 10]);
``````