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/img/dsp_lp_filter/octave_freqz.png | Bin 0 -> 43657 bytes md/img/dsp_lp_filter/octave_freqz_prev.png | Bin 0 -> 43859 bytes md/img/dsp_lp_filter/signal_post.png | Bin 0 -> 36635 bytes md/img/dsp_lp_filter/signal_post_fft.png | Bin 0 -> 25966 bytes md/img/dsp_lp_filter/signal_post_fft_prev.png | Bin 0 -> 18783 bytes md/img/dsp_lp_filter/signal_post_prev.png | Bin 0 -> 72447 bytes md/img/dsp_lp_filter/signal_pre.png | Bin 0 -> 42094 bytes md/img/dsp_lp_filter/signal_pre_fft.png | Bin 0 -> 43114 bytes md/img/dsp_lp_filter/signal_pre_fft_prev.png | Bin 0 -> 43625 bytes md/img/dsp_lp_filter/signal_pre_prev.png | Bin 0 -> 104134 bytes md/notes/syscalls.md | 3 +- md/styles/main.css | 9 +- md/writeup.md | 1 + md/writeup/dsp_lp_filter.md | 270 ++++++++++++++++++++++++++ md/writeup/using_iptables.md | 10 +- 15 files changed, 288 insertions(+), 5 deletions(-) create mode 100644 md/img/dsp_lp_filter/octave_freqz.png create mode 100644 md/img/dsp_lp_filter/octave_freqz_prev.png create mode 100644 md/img/dsp_lp_filter/signal_post.png create mode 100644 md/img/dsp_lp_filter/signal_post_fft.png create mode 100644 md/img/dsp_lp_filter/signal_post_fft_prev.png create mode 100644 md/img/dsp_lp_filter/signal_post_prev.png create mode 100644 md/img/dsp_lp_filter/signal_pre.png create mode 100644 md/img/dsp_lp_filter/signal_pre_fft.png create mode 100644 md/img/dsp_lp_filter/signal_pre_fft_prev.png create mode 100644 md/img/dsp_lp_filter/signal_pre_prev.png create mode 100644 md/writeup/dsp_lp_filter.md (limited to 'md') diff --git a/md/img/dsp_lp_filter/octave_freqz.png b/md/img/dsp_lp_filter/octave_freqz.png new file mode 100644 index 0000000..e99293b Binary files /dev/null and b/md/img/dsp_lp_filter/octave_freqz.png differ diff --git a/md/img/dsp_lp_filter/octave_freqz_prev.png b/md/img/dsp_lp_filter/octave_freqz_prev.png new file mode 100644 index 0000000..feb5c70 Binary files /dev/null and b/md/img/dsp_lp_filter/octave_freqz_prev.png differ diff --git a/md/img/dsp_lp_filter/signal_post.png b/md/img/dsp_lp_filter/signal_post.png new file mode 100644 index 0000000..72f967d Binary files /dev/null and b/md/img/dsp_lp_filter/signal_post.png differ diff --git a/md/img/dsp_lp_filter/signal_post_fft.png b/md/img/dsp_lp_filter/signal_post_fft.png new file mode 100644 index 0000000..2644c76 Binary files /dev/null and b/md/img/dsp_lp_filter/signal_post_fft.png differ diff --git a/md/img/dsp_lp_filter/signal_post_fft_prev.png b/md/img/dsp_lp_filter/signal_post_fft_prev.png new file mode 100644 index 0000000..33b0465 Binary files /dev/null and b/md/img/dsp_lp_filter/signal_post_fft_prev.png differ diff --git a/md/img/dsp_lp_filter/signal_post_prev.png b/md/img/dsp_lp_filter/signal_post_prev.png new file mode 100644 index 0000000..7941342 Binary files /dev/null and b/md/img/dsp_lp_filter/signal_post_prev.png differ diff --git a/md/img/dsp_lp_filter/signal_pre.png b/md/img/dsp_lp_filter/signal_pre.png new file mode 100644 index 0000000..90a34a5 Binary files /dev/null and b/md/img/dsp_lp_filter/signal_pre.png differ diff --git a/md/img/dsp_lp_filter/signal_pre_fft.png b/md/img/dsp_lp_filter/signal_pre_fft.png new file mode 100644 index 0000000..d522b22 Binary files /dev/null and b/md/img/dsp_lp_filter/signal_pre_fft.png differ diff --git a/md/img/dsp_lp_filter/signal_pre_fft_prev.png b/md/img/dsp_lp_filter/signal_pre_fft_prev.png new file mode 100644 index 0000000..13a327a Binary files /dev/null and b/md/img/dsp_lp_filter/signal_pre_fft_prev.png differ diff --git a/md/img/dsp_lp_filter/signal_pre_prev.png b/md/img/dsp_lp_filter/signal_pre_prev.png new file mode 100644 index 0000000..bab7fcd Binary files /dev/null and b/md/img/dsp_lp_filter/signal_pre_prev.png differ diff --git a/md/notes/syscalls.md b/md/notes/syscalls.md index 4c13b36..41575a7 100644 --- a/md/notes/syscalls.md +++ b/md/notes/syscalls.md @@ -356,4 +356,5 @@ line that should be syscall source code lines 2. [http://docs.cs.up.ac.za](http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html) 3. [http://syscalls.kernelgrok.com](http://syscalls.kernelgrok.com/) 4. [http://man7.org](http://man7.org/linux/man-pages/man2/syscalls.2.html) -5. [https://filippo.io](https://filippo.io/linux-syscall-table/) \ No newline at end of file +5. [https://filippo.io](https://filippo.io/linux-syscall-table/) +6. [https://fedora.juszkiewicz.com.pl/syscalls.html](https://fedora.juszkiewicz.com.pl/syscalls.html) \ No newline at end of file diff --git a/md/styles/main.css b/md/styles/main.css index db67eb6..bd6716a 100644 --- a/md/styles/main.css +++ b/md/styles/main.css @@ -43,4 +43,11 @@ table tr:nth-child(odd) table tr:nth-child(even) { -} \ No newline at end of file +} + +p a img +{ + margin: auto; + width: 80%; + display: block; +} diff --git a/md/writeup.md b/md/writeup.md index d6d83f6..3bddfc9 100644 --- a/md/writeup.md +++ b/md/writeup.md @@ -17,6 +17,7 @@ title: Writeup page [Kernel:Hello World](writeup/kernel_hello_world.md) [Kernel:Debug messages](writeup/kernel_debug_messages.md) [Kernel:/dev/hwrng](writeup/kernel_dev_hwrng.md) +[DSP:Low-pass filter](writeup/dsp_lp_filter.md) ## Projects 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