Next  |  Prev  |  Up  |  Top  |  Index  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search


Acyclic FFT Convolution in Matlab or Octave

The following example illustrates the implementation of acyclic convolution using the FFT in Matlab or Octave:

% convexample.m

x = [1 2 3 4];
h = [1 1 1];

nx = length(x);
nh = length(h);
nfft = 2^nextpow2(nx+nh-1)
xzp = [x, zeros(1,nfft-nx)];
hzp = [h, zeros(1,nfft-nh)];
X = fft(xzp);
H = fft(hzp);

Y = H .* X;
format bank;
y = real(ifft(Y)) % zero-padded result
yt = y(1:nx+nh-1) % trim and print
yc = conv(x,h)    % for comparison
Program output:

nfft = 8
y =
  1.00  3.00  6.00  9.00  7.00  4.00  0.00  0.00
yt =
  1.00  3.00  6.00  9.00  7.00  4.00
yc =
     1     3     6     9     7     4


Next  |  Prev  |  Up  |  Top  |  Index  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

[How to cite this work] [Order a printed hardcopy]

``Spectral Audio Signal Processing'', by Julius O. Smith III, (August 2008 Draft).
Copyright © 2008-08-13 by Julius O. Smith III
Center for Computer Research in Music and Acoustics (CCRMA),   Stanford University
CCRMA  [Automatic-links disclaimer]