FFT-based FIR filtering using overlap-add method
y = fftfilt(b,x)
y = fftfilt(b,x,n)
y = fftfilt(gpuArrayb,gpuArrayX,n)
fftfilt filters data using the efficient FFT-based method of overlap-add, a frequency domain filtering technique that works only for FIR filters.
y = fftfilt(b,x) filters the data in vector x with the filter described by coefficient vector b. It returns the data vector y. The operation performed by fftfilt is described in the time domain by the difference equation:
An equivalent representation is the z-transform or frequency domain description:
By default, fftfilt chooses an FFT length and data block length that guarantee efficient execution time.
If x is a matrix, fftfilt filters its columns. If b is a matrix, fftfilt applies the filter in each column of b to the signal vector x. If b and x are both matrices with the same number of columns, the i-th column of b is used to filter the i-th column of x.
y = fftfilt(b,x,n) uses n to determine the length of the FFT. See Algorithms for information.
y = fftfilt(gpuArrayb,gpuArrayX,n) filters the data in the gpuArray object, gpuArrayX, with the FIR filter coefficients in the gpuArray, gpuArrayb. See Establish Arrays on a GPU for details on gpuArray objects. Using fftfilt with gpuArray objects requires Parallel Computing Toolbox™ software and a CUDA-enabled NVIDIA GPU with compute capability 1.3 or above. See http://www.mathworks.com/products/parallel-computing/requirements.html for details. The filtered data, y, is a gpuArray object. See Overlap-Add Filtering on the GPU for example of overlap-add filtering on the GPU.
fftfilt works for both real and complex inputs.
When the input signal is relatively large, it is advantageous to use fftfilt instead of filter, which performs N multiplications for each sample in x, where N is the filter length. fftfilt performs 2 FFT operations — the FFT of the signal block of length L plus the inverse FT of the product of the FFTs — at the cost of
where L is the block length. It then performs L pointwise multiplications for a total cost of
L+L*log2(L) = L*(1+log2(L))
multiplications. The cost ratio is therefore
L*(1+log2(L))/(N*L) = (1+log2(L))/N
which is approximately log2(L)/N.
Therefore, fftfilt becomes advantageous when log2(L) is less than N.
Show that the results from fftfilt and filter are identical:
b = [1 2 3 4]; x = [1 zeros(1,99)]'; norm(fftfilt(b,x) - filter(b,1,x))
The following example requires Parallel Computing Toolbox software and a CUDA-enabled NVIDIA GPU with compute capability 1.3 or above. See http://www.mathworks.com/products/parallel-computing/requirements.html for details.
Create a signal consisting of a sum of sine waves in white Gaussian additive noise. The sine wave frequencies are 2.5, 5, 10, and 15 kHz. The sampling frequency is 50 kHz.
Fs = 50e3; t = 0:1/Fs:10-(1/Fs); x = cos(2*pi*2500*t)+0.5*sin(2*pi*5000*t)+0.25*cos(2*pi*10000*t)+0.125*sin(2*pi*15000*t)+randn(size(t));
Design a lowpass FIR equiripple filter using fdesign.lowpass.
d = fdesign.lowpass('Fp,Fst,Ap,Ast',5500,6000,0.5,50,50e3); Hd = design(d); B = Hd.Numerator;
Filter the data on the GPU using the overlap-add method. Put the data on the GPU using gpuArray. Return the output to the MATLAB® workspace using gather and plot the power spectral density estimate of the filtered data.
y = fftfilt(gpuArray(B),gpuArray(x)); periodogram(gather(y),rectwin(length(y)),length(y),50e3);
fftfilt uses fft to implement the overlap-add method , a technique that combines successive frequency domain filtered blocks of an input sequence. fftfilt breaks an input sequence x into length L data blocks, where L must be greater than the filter length N.
and convolves each block with the filter b by
y = ifft(fft(x(i:i+L-1),nfft).*fft(b,nfft));
where nfft is the FFT length. fftfilt overlaps successive output sections by n-1 points, where n is the length of the filter, and sums them.
fftfilt chooses the key parameters L and nfft in different ways, depending on whether you supply an FFT length n and on the lengths of the filter and signal. If you do not specify a value for n (which determines FFT length), fftfilt chooses these key parameters automatically:
If length(x)is greater than length(b), fftfilt chooses values that minimize the number of blocks times the number of flops per FFT.
If length(b) is greater than or equal to length(x), fftfilt uses a single FFT of length
2^nextpow2(length(b) + length(x) - 1)
This essentially computes
y = ifft(fft(B,nfft).*fft(X,nfft))
If you supply a value for n, fftfilt chooses an FFT length, nfft, of 2^nextpow2(n)and a data block length of nfft - length(b) + 1. If n is less than length(b), fftfilt sets n to length(b).
 Oppenheim, A.V., and R.W. Schafer. Discrete-Time Signal Processing, Prentice-Hall, 1989.