Digital Signal Processing Basics - VTAstrobotics/Documentation GitHub Wiki
Do note that this various parts of this is covered in various Sophomore-Senior ECE classes
Contents
Prerequisites
The basics of Filtering are covered below, but having taken Signals and Systems is essential to understanding much of this. DOWNLOAD MATLAB SIGNAL PROCESSING TOOLBOX!!!
Filtering
Goals of Filtering
Every signal is composed of various components with different frequencies and amplitudes. The goal of filtering is to suppress the components with unwanted frequencies, usually noise and allow the relevant signal to get through.
Types of Filters
Filters are first defined by what frequencies they attenuate. Ones that suppress high frequencies relative to the passband are high pass, ones that do the same for low frequencies are low pass. Filters that suppress frequencies that are both above and below a frequency range are bandpass, whereas those that suppress frequencies between two points are band reject.
Then there is the mathematics of the filters (what polynomial they use). The main impact this has is the shape of the bode plot and where ripples are. They are summarized below in order of how quickly they transition from passband to stopband. Do note that these are all IIR filters.
Filter Type | Passband Ripple | Stopband Ripple | Attenuation Speed Rank | Notes |
---|---|---|---|---|
Bessel | No | No | 5 | Linear phase delay in transition band |
Butterworth | No | No | 4 | -- |
Chebyshev Type 1 | Yes | No | 2 | -- |
Chebyshev Type 2 | No | Yes | 2 | -- |
Elliptic | Yes | Yes | 1 | -- |
Do note that for 90% of applications, Butterworth and Chebyshev have sufficient attenuation speeds.
Digital Signal Processing
Digital Signal Processing's goal is to filter using a computer instead of analog components (resistors, capacitors, inductors). Instead of the number of components that are used being a limiting factor, the issue is generally precision (in terms of bits) and memory usage. Do note that you need to know your system's sampling frequency in addition to the input and output frequencies of the system. For digital filters there are two subtypes of filters: Finite Impulse Response (FIR) and Infinite Impulse Response (IIR), compared below.
Category | IIR | FIR |
---|---|---|
Memory Efficiency | Better | Worse |
Computational Efficiency | Better | Worse |
Phase in Passband | Varies | Linear |
Distortion | More distortion | Less distortion |
Stability | Could be an issue | Always stable |
Fixed Point vs Floating Point Floating point vs fixed point is probably one of the most irritating aspects of DSP. Floating point is in every sense of the word better, at least in terms of filtering. However, it requires far more expensive microcontrollers compared to floating point, and consumes far more power.
How to
Designing Your Filter
-
You need the type of filter (low pass, high pass, etc.), frequency range you want preserved, the frequency range you want removed, the sampling frequency of the system, the order of the filter, FIR or IIR and the shape of the bode plot (butterworth, chebyshev, etc.).
-
Use Matlab to generate the coefficients of your filter. Look at MATLAB's documentation to use the proper functions.
-
Simulate your filter's frequency response with the coefficients.
-
If you are using Fixed Point you should consider making your larger filter (order 4+) into a series of smaller second order stages to reduce error and distortion. Matlab Function tf2sos. Do note that if you use tf2sos you will see need to compensate the gain at the end (as it does introduce gain).
Example of design code:
s = 100000; % replace 100000 with your sampling frequency
[b,a] = butter(ORDER,[LOW_CUTOFF HIGH_CUTOFF]*2/s,'bandpass'); % replace this with whatever function & filter you're actually using
freqz(b,a,[],s) % displays the bode plot
[ss,gn] = tf2sos(b,a);% gets it in 2 second order stages
numers = [conv(ss(1,1:3),ss(2,1:3))*gn;nm] % displays the numerators
denoms = [conv(ss(1,4:6),ss(2,4:6));dn] % displays the denominators
-
From here you will likely need to implement it in direct form II to save memory. https://www.dsprelated.com/freebooks/filters/Direct_Form_II.html Do note that Direct form I (the standard difference equation) can be more reliable if overflow is an issue in fixed point. But you can also just reduce the number of precision bits because you're in fixed point.
-
That output if everything went flawlessly should be the filter's output. I suggest simulating this to debug.