Fitting Filters to Measured Amplitude Response Data Using invfreqz in Matlab
In the 1980s I wrote the matlab function invfreqz that implements a fast (FFT-based) equation-error method for recursive filter design given samples of the desired frequency response. This method designs causal filters, fitting both the phase and magnitude of the desired (complex) frequency response. As a result, one must be careful about the specified phase response, especially when it doesn't matter! This article gives some pointers on this topic.
The case I see most often is where there are no phase data at all. For example, a filter response may be specified simply as a desired (real) gain at various frequencies, i.e., only the amplitude response is specified. The easiest choice of phase response in this case is zero, but that would correspond to an impulse response that is symmetric about time zero, and causal filters cannot produce any response before time zero. In this case, phase-sensitive filter-design methods such as invfreqz will generally give their best results for minimum phase in place of zero phase. In other words, we need to convert our desired amplitude response to the corresponding minimum-phase frequency response (whose magnitude equals the original desired amplitude response).
A commonly used method for computing the minimum-phase frequency response from its magnitude is the cepstral method. The steps are as follows:
- Interpolate the amplitude response samples from 0 to half the sampling rate, if necessary, and resample to a uniform "FFT frequency axis", if necessary. Denote the real, sampled amplitude response by S(k).
- Perform an inverse FFT of log(S) to obtain the real cepstrum of s, denoted by c(n).
- Fold the noncausal portion of c(n) onto its causal portion.
- Perform a forward FFT, followed by exponentiation to obtain the minimum phase frequency response Sm(k), where now sm(n) is causal, and |Sm(k)|=S(k).
These steps are illustrated by the matlab example below (tested in Octave, version 3.2.4, on Mac OS X). Copy-paste it and give it a try!
NZ = 1
; % number of ZEROS in the filter to be designed
NP = 4
; % number of POLES in the filter to be designed
NG = 10
; % number of gain measurements
; % lowest measurement frequency (Hz)
fmax = 3000
; % highest measurement frequency (Hz)
fs = 10000
; % discrete-time sampling rate
Nfft = 512
; % FFT size to use
df = (
; % uniform log-freq spacing
f = fmin
* df .^ (0
; % measurement frequency axis
% Gain measurements (synthetic example = triangular amp response):
Gdb = 10
; % between 0 and 10 dB gain
% Must decide on a dc value.
% Either use what is known to be true or pick something "maximally
% smooth". Here we do a simple linear extrapolation:
dc_amp = Gdb(1)
% Must also decide on a value at half the sampling rate.
% Use either a realistic estimate or something "maximally smooth".
% Here we do a simple linear extrapolation. While zeroing it
% is appealing, we do not want any zeros on the unit circle here.
Gdb_last_slope = (
nyq_amp = Gdb(
+ Gdb_last_slope * (
Gdbe = [
dc_amp, Gdb, nyq_amp]
fe = [0
NGe = NG+2
% Resample to a uniform frequency grid, as required by ifft.
% We do this by fitting cubic splines evaluated on the fft grid:
Gdbei = spline(
; % say `help spline'
fk = fs*[0
/Nfft; % fft frequency grid (nonneg freqs)
Gdbfk = ppval(
; % Uniformly resampled amp-resp
; ylabel('Magnitude (dB)')
title(['Measured and Extrapolated/Interpolated/Resampled '
Ns = length(
Sdb = [
; % install negative-frequencies
S = 10
; % convert to linear magnitude
s = ifft(
; % desired impulse response
s = real(
; % any imaginary part is quantization noise
tlerr = 100
disp(sprintf(['Time-limitedness check: Outer 20%% of impulse ' ...
'response is %0.2f %% of total rms']
% = 0.02 percent
tlerr>1.0 % arbitrarily set 1% as the upper limit allowed
error('Increase Nfft and/or smooth Sdb')
; title('Impulse Response')
c = ifft(
; % compute real cepstrum from log magnitude spectrum
% Check aliasing of cepstrum (in theory there is always some):
caliaserr = 100
disp(sprintf(['Cepstral time-aliasing check: Outer 20%% of ' ...
'cepstrum holds %0.2f %% of total rms']
% = 0.09 percent
caliaserr>1.0 % arbitrary limit
error('Increase Nfft and/or smooth Sdb to shorten cepstrum')
% Fold cepstrum to reflect non-min-phase zeros inside unit circle:
% If complex:
% cf = [c(1), c(2:Ns-1)+conj(c(Nfft:-1:Ns+1)), c(Ns), zeros(1,Nfft-Ns)];
cf = [
Cf = fft(
; % = dB_magnitude + j * minimum_phase
Smp = 10
; % minimum-phase spectrum
Smpp = Smp(1
; % nonnegative-frequency portion
wt = 1
; % typical weight fn for audio
wk = 2
Hh = freqz(
title('Magnitude Frequency Response')
Rate this code snippet:
posted by Julius Orion Smith III
Julius Smith's background is in electrical engineering (BS Rice 1975, PhD Stanford 1983). He is presently Professor of Music and Associate Professor (by courtesy) of Electrical Engineering at Stanford's Center for Computer Research in Music and Acoustics (CCRMA)
, teaching courses and pursuing research related to signal processing applied to music and audio systems. See http://ccrma.stanford.edu/~jos/