Simulated Sine-Wave Analysis in Matlab
In this section, we will find the frequency response of the simplest lowpass filter
% swanalmain.m - matlab program for simulated sine-wave
% analysis on the simplest lowpass filter:
%
% y(n) = x(n)+x(n-1)}
Figure 2.3: Main matlab program for computing the frequency response of the simplest low-
pass filter by means of simulated sine-wave analysis.
function [gains,phases] = swanal(t,f,B,A)
% SWANAL - Perform sine-wave analysis on filter B(z)/A(z)
Next, the one-sample start-up transient is removed from the filter output signal y to
form the ``cropped'' signal yss (`` steady state''). The final task is to measure the
amplitude and phase of the yss. Output amplitude estimation is done in swanal by
the line
[ampout,peakloc] = max(abs(yss));
Note that the peak amplitude found in this way is approximate, since the true peak of
the output sinusoid generally occurs between samples. We will find the output
amplitude much more accurately in the next two sections. We store the index of the
amplitude peak in peakloc so it can be used to estimate phase in the next step. Given
the output amplitude ampout, the amplitude response of the filter at
frequency f(k) is given by
gains(k) = ampout/ampin;
The last step of swanal in Fig.2.4 is to estimate the phase of the cropped filter output
signal yss. Since we will have better ways to accomplish this later, we use a
simplistic method here based on inverting the sinusoid analytically:
phaseout = acos(yss(peakloc)/ampout) ...
- 2*pi*f(k)*(peakloc+ntransient-1);
phaseout = mod2pi(phaseout); % reduce to [-pi,pi)
Again, this is only an approximation since peakloc is only accurate to the nearest
sample. The mod2pi utility reduces its scalar argument to the range ,3.6 and is
listed in Fig.2.6.
function [y] = mod2pi(x)
% MOD2PI - Reduce x to the range [-pi,pi)
y=x;
twopi = 2*pi;
while y >= pi, y = y - twopi; end
while y < -pi, y = y + twopi; end
In summary, the sine-wave analysis measures experimentally the gain and phase-shift
of the digital filter at selected frequencies, thus measuring the frequency response of
the filter at those frequencies. It is interesting to compare these experimental results
with the closed-form expressions for the frequency response derived in §1.3.2. From
Equations (1.6-1.7) we have
The need for interpolation is lessened greatly if the sampling rate is chosen to
be unrelated to the test frequencies (ideally so that the number of samples in
each sinusoidal period is an irrational number). Figure 2.8 shows the measured
and theoretical results obtained by changing the highest test
frequency fmax from to , and the number of samples in each
test sinusoid tmax from to . For these parameters, at least one sample
falls very close to a true peak of the output sinusoid at each test frequency.
Using the theory presented in later chapters, we will be able to compute very
precisely the frequency response of any LTI digital filter without having to
resort to bandlimited interpolation (for measuring amplitude response) or time-
delay estimation (for measuring phase).
``Introduction to Digital Filters with Audio Applications'', by Julius O. Smith III, (September 2007 Edition).
Copyright © 2010-12-28 by Julius O. Smith III
Center for Computer Research in Music and Acoustics (CCRMA), Stanford University