From 2db4ef8315e7f7ea81d480f539de274f4e636c82 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Sun, 21 May 2017 17:42:50 +0100 Subject: Added new post about low pass filter example, and example usage --- md/writeup/dsp_lp_filter.md | 270 +++++++++++++++++++++++++++++++++++++++++++ md/writeup/using_iptables.md | 10 +- 2 files changed, 277 insertions(+), 3 deletions(-) create mode 100644 md/writeup/dsp_lp_filter.md (limited to 'md/writeup') diff --git a/md/writeup/dsp_lp_filter.md b/md/writeup/dsp_lp_filter.md new file mode 100644 index 0000000..13260c7 --- /dev/null +++ b/md/writeup/dsp_lp_filter.md @@ -0,0 +1,270 @@ +title: DSP Low-pass FIR filter + +# Low FIR pass-filer + +## Intro + +Some fast intro to low pass filters. Let's say this is notes is just to get into low pass filters without detailed explanations. Just play with tools like Octave and get sense of how it works. Well here is used Inglish so your eyes gona bleed. + +So what is low-pass filter? In DSP its algo that can filter some array of values or data and reduce amount of high frequency fluctuations. For example if you recording movements of your hand and your hand is shaking(shaking very fast), low pass filter can "remove" that from your recorded data. + +If you have recording sound were you noticed that flying mosquito is recorded then with is you can reduce noise from mosquito in recorded sound. + +If you just use filtr as a function then main params is how good are your filter and cutoff frequency. Good filter have higher number of filter coefficients with are determined from filter order. Cutoff frequency is set to normalised valueshtml from 0.0 ... 1.0. Set them according which part should be filtered. + +## Octave examples + +### Draw first filter characteristics + +``` +fir1([filter order],[cutoff freq]) +``` + +Draw in Octave simple 40th order filter, as in Octave example + +``` +freqz (fir1 (40, 0.3)); +``` + +![octave freqz result](/img/dsp_lp_filter/octave_freqz.png) + + +Play with first param of fir1 and you will see how it affects characteristics of filter. Try filter increase filter order from 1,2,3,4 and then go to higher numbers like 100. + +You will see 2 pictures. First picture show frequency response of filter. + +Good low-pass digital filter should have right side lower than left side of picture. And transition from left to right should be sharp as possible. We can see visually if this requirements are met. If filter order is increased more sharp transition becomes. + +Second picture showing phase response. Well for now it's not so "important". + +### Create signal and filter it + +Let's create some signal from couple of frequencies, like +100,400,750,1000,1200,1500 then let's do FFT to see what we have. + +![source signal](/img/dsp_lp_filter/signal_pre.png) + +![source signal fft](/img/dsp_lp_filter/signal_pre_fft.png) + +__Programm__ +``` +Fs = 2000; +t = 0:1/Fs:1-1/Fs; + +x = cos(2*pi*100*t) + cos(2*pi*400*t) + cos(2*pi*750*t) \ + + cos(2*pi*1000*t) + cos(2*pi*1200*t) + cos(2*pi*1500*t); + +N = length(x); +xdft = fft(x); +xdft = xdft(1:N/2+1); +psdx = (1/(Fs*N)) * abs(xdft).^2; +psdx(2:end-1) = 2*psdx(2:end-1); +freq = 0:Fs/length(x):Fs/2; + +figure(1) +plot(freq,10*log10(psdx)) +grid on +title('Periodogram Using FFT') +xlabel('Frequency (Hz)') +ylabel('Power/Frequency (dB/Hz)') +``` + +Lets filter out with cutoff frequency 1000kHz. Should get rid of +1000,1200,1500 frequencies in resulting FFT. + +![output signal](/img/dsp_lp_filter/signal_post.png) + +![output signal fft](/img/dsp_lp_filter/signal_post_fft.png) + +``` +%filter +%sampling frequency +Fs = length(x); +Fnyq = Fs/2; +fc = 1000; +fdev = 20; +%[b,a]=butter(8, [900/Fs,1100/Fs]); +hc = fir1(41,fc/Fs); +f_filt = filter( hc, 1, x ); + +N = length(f_filt); +xdft = fft(f_filt); +xdft = xdft(1:N/2+1); +psdx = (1/(Fs*N)) * abs(xdft).^2; +psdx(2:end-1) = 2*psdx(2:end-1); +freq = 0:Fs/length(x):Fs/2; + +figure(2); +plot( freq,10*log10(psdx) ); +title("filtered fft"); + +figure(3); +freqz(fir1(41,0.5)); +``` + +## Calculate coefficients + +## Code example + +Octave allows to see what filter properties is, also can choose appropriate filter type. Find how precise filter should be. And also test data on some signal sample. Now lets implement that with C. + +__Define FIR filter coeficients__ +```c +#define FIR_SIZE 12 + +double fir_coef[FIR_SIZE] = +{ + -0.0044384, -0.0041841, 0.0130183, 0.0746628, + 0.1720210, 0.2489204, 0.2489204, 0.1720210, + 0.0746628, 0.0130183, -0.0041841, -0.0044384 +}; +``` + +__FIR Filter__ + +Applying filter coefficients to input signal, place where filtering is happening. + + + + + + y + + ( + n + ) + + = + + + + + k + = + 0 + + + N + - + 1 + + + + + h + + ( + k + ) + + x + + ( + n + - + k + ) + + + + + + +```c +for (k=0;k +#include +#include + +#define FIR_SIZE 12 + +double fir_coef[FIR_SIZE] = +{ + -0.0044384, -0.0041841, 0.0130183, 0.0746628, + 0.1720210, 0.2489204, 0.2489204, 0.1720210, + 0.0746628, 0.0130183, -0.0041841, -0.0044384 +}; + +#define MAX_INPUT_LEN 80 +#define BUFFER_LEN (FIR_SIZE-1+MAX_INPUT_LEN) +double insamp[BUFFER_LEN]; + +int fir1filter(double *coeffs, double *input, double *output, + int length, int filter_length) +{ + double acc; + double *coeffp; + double *inputp; + int n; + int k; + + memcpy(&insamp[filter_length-1], input, length*sizeof(double)); + + for (n=0; n