Google

main
dspblog
cyclic_signals
FFT_interpolation
FFT_interpolation_how_does_it_work
FFT_smoothness_cyclic
discrete_time_reconstruction
discrete_time_reconstruction2
Nyquist_on_the_edge
FFT_delay_special_case
Fourier_reconstruction
pulse_and_Nyquist
FFT_complexError
matlabOctave
matlab_upsampling
matlab_downsampling
FFT_delay
FFT_filter_example
FFT_interpolation_example
FFT_bin_frequencies
fit_signal
FFT_peaksearch_audio_example
matlab_binary_readwrite
Octavesvg
C
FFTW_example
looprecord
SNR
SNR3
SNR_example_96kAudio
SNR_FFT_correlation_example
lua
luagpib
luasplit
luadump
mnoofltk
wxLuaDll
wxLua_loadAsDll
wxLua_HelloWorld
wxLua_simpleButton
wxLua_resourceManagement
wxLua_XMLparser
DSP
IQ_LO
IQ_LO_2
optimum_receiver
DSP_basics
sampleRateChange_terms
dirac_pulse
freqresp_s
zfilter_example
freqresp_z
freqresp_z_sign
misc
zero_forcing_equalizer_example
nonminphase_inverse
periodic_spectrum
lagrange_multipliers
Entropy
RC_chopper
TRex450_setup
EP100
EP100SE
EP100Gremlins
essential_spares
tail_rotor
motoradjustment
blade_balancing
blade_repair
GAUI_SAE12A
Walkera43


valid html (click to verify)



prevupnextdisable ads

Discrete-time (digital) IIR filter, Matlab example

Summary

Matlab example program that designs a discrete-time IIR filter and plots its frequency response.

Example program

The program generates an example filter, calculates the frequency response on a logarithmic axis, and plots it (figure 1, 2).
It also plots |H(z)| for frequencies far beyond the sampling frequency, showing that the frequency response is periodic with the sampling frequency (figure 3).



Figure 1: Amplitude response

Figure 2: Phase response

Figure 3: Periodic frequency response
Download
% ***********************************************************************
% Discrete-time filter example
% © Markus Nentwig 2007
% This program is provided without any warranty.
% ***********************************************************************
close all; clear all;

% Filter design parameters
order=5;
fCutoff=2000;

fSample=44100;
fCutoff=fCutoff/(fSample/2);

% ***********************************************************************
% Design a filter
% ***********************************************************************
if 0
  [b,a]=butter(order, fCutoff);
elseif 1
  ripple_dB=0.5;
  [b,a]=cheby1(order, ripple_dB, fCutoff);
elseif 0
  ripple_dB=0.5;
  stopband_dB=60;
  [b,a]=ellip(order, ripple_dB, stopband_dB, fCutoff);
end

% ***********************************************************************
% Build a logarithmic frequency axis
% ***********************************************************************
f1=10;
f2=fSample/2;
nPoints=3000;
fbase=exp(linspace(log(f1), log(f2), nPoints));
fbase=fbase/(fSample);

% ***********************************************************************
% Numerator and denominator are polynomials in z=exp(i*omega)
% ***********************************************************************
H_num=polyval(b, exp(i*2*pi*fbase));
H_denom=polyval(a, exp(i*2*pi*fbase));
% H is the complex gain of the filter at any given frequency
H=H_num ./ H_denom;

% ***********************************************************************
% - Convert to dB. H could be a voltage or current => use 20 log |H|
% - Obtain phase in degrees
% - plot
% ***********************************************************************
gain_dB=20*log10(abs(H)+1e-15); % don't show below -300 dB
phase_deg=unwrap(arg(H))/pi*180;
figure(); 
semilogx(fbase*fSample, gain_dB, '+'); xlabel('f/Hz'); ylabel('|H|/dB'); grid on;
title('amplitude response');

figure();
semilogx(fbase*fSample, phase_deg, '+'); xlabel('f/Hz'); ylabel('phase/deg'); grid on;
title('phase response');

% ***********************************************************************
% Build a linear frequency axis
% ***********************************************************************
fbase=linspace(0, 4*fSample, nPoints);
fbase=fbase/fSample;

% ***********************************************************************
% Evaluate frequency response as before
% ***********************************************************************
H_num=polyval(b, exp(i*2*pi*fbase));
H_denom=polyval(a, exp(i*2*pi*fbase));
H=H_num ./ H_denom;

% ***********************************************************************
% Plot as before, but on linear frequency axis
% ***********************************************************************
gain_dB=20*log10(abs(H)+1e-15); % don't show below -300 dB
figure(); plot(fbase*fSample, gain_dB, '+'); xlabel('f/Hz'); ylabel('|H|/dB'); grid on;
title('amplitude response');


prevupnextdisable ads

© Markus Nentwig 2007-2008
The content of this page is provided without any warranty and may not be reproduced without permission.

Comments? Questions?

Please send me a mail! mnentwig@elisanet.fi