# MATLAB- ifft of simple lowpass filter returns NaN

55 views (last 30 days)
Nathan Monroe on 7 Mar 2017
Commented: Nathan Monroe on 7 Mar 2017
Hello, I seem to be having issues using MATLAB's fft and ifft functions, when simulating a simple electrical circuit. Generally the strategy I've been taking is to fft the incoming time-domain signal, multiply the result by the frequency response of the circuit, and ifft the product to produce the time-domain output. However ifft seems to be producing NaN for the real part (and a small complex part). Any idea why this may be occurring and what to do about it? A simple example is below, using a first order RC butterworth filter. I'm really confused as to why this may be happening. I realize there are other ways to simulate such a simple system, however I'm applying this method to much more complex systems with existing frequency-domain models. Thanks!
clear all
Fs = 100;
n=1000;
t = (1/Fs:1/Fs:10); %time
test1 = cos(2*pi*5*t); %test funcion, 10Hz cosine
X = fft(test1,n); %1k pt fft
dF = Fs/n; %0.1Hz bin width
f = (dF*(0:n-1))-Fs/2; %frequency vector, centered around zero
c=1e-4;
r=1e3;
tf = (1./(j*2*pi*f*c))./(r+(1./(j*2*pi*f*c))); %filter frequency response
X_filtered = X.*tf;
out = ifft(X_filtered,n);

David Goodmanson on 7 Mar 2017
Hello Nathan, I believe the problem is because the frequency grid contains the point f=0, and your expression for tf comes up with nan at that one point. If you write tf as
tf = 1./(1+j*2*pi*f*r*c); %filter frequency response
you should get better results. (Or find the nan in the first expression and change it to 1).
Nathan Monroe on 7 Mar 2017
This did the trick, thanks!

### Categories

Find more on Multirate Signal Processing 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!