Does anybody have working code(i'm Beginner) for an lowpass filter in Pascal?
I need this for ADC Measurement
lowpass filter
Re: lowpass filter
Hi,
sorry for not posting earlier, just noticed your question. Here is a simple average filter I used in the PPM project: https://libstock.mikroe.com/projects/vi ... -with-leds
The code below is a modified version of the input filter (see PreFilter). There are actually two filters (for left and right channels), managed by the same procedure.
The filter from this code, has the same integration time, both for rising and falling inputs, as oposed to the code from the PPM project, where these times are not the same by design.
HTH
sorry for not posting earlier, just noticed your question. Here is a simple average filter I used in the PPM project: https://libstock.mikroe.com/projects/vi ... -with-leds
The code below is a modified version of the input filter (see PreFilter). There are actually two filters (for left and right channels), managed by the same procedure.
The filter from this code, has the same integration time, both for rising and falling inputs, as oposed to the code from the PPM project, where these times are not the same by design.
Code: Select all
var
ValLeft, ValRight: Word; //from ADC
PreFilterLeft, PreFilterRight: array[0..15] of Word; //use power of 2, for easy division using shr.
PreFilterOutLeft, PreFilterOutRight: DWord;
PreTimeBaseCounter: Byte;
procedure PreFilter;
var
i: Byte;
begin
//shift registers with 16 samples, executed on each 4'th timebase iteration:
if PreTimeBaseCounter = 0 then
begin
for i := 0 to 14 do //15 - 1
PreFilterLeft[i] := PreFilterLeft[i + 1];
end;
if PreTimeBaseCounter = 0 then
begin
for i := 0 to 14 do //15 - 1
PreFilterRight[i] := PreFilterRight[i + 1];
end;
//timebase for shift register (speed affects filter response):
Inc(PreTimeBaseCounter);
if PreTimeBaseCounter >= 4 then
PreTimeBaseCounter := 0;
//shift sampled data into shift register:
PreFilterLeft[15] := ValLeft;
PreFilterRight[15] := ValRight;
//average samples:
PreFilterOutLeft := 0;
PreFilterOutRight := 0;
for i := 0 to 15 do
begin
PreFilterOutLeft := PreFilterOutLeft + PreFilterLeft[i];
PreFilterOutRight := PreFilterOutRight + PreFilterRight[i];
end;
PreFilterOutLeft := PreFilterOutLeft shr 4; //shr 4, because 2^4 = 16 samples
PreFilterOutRight := PreFilterOutRight shr 4; //use shift, instead of div, because it's faster
end;
begin
Delay_ms(100);
ADC_Init;
repeat
ValLeft := ADC_Get_Sample(0);
ValRight := ADC_Get_Sample(1);
PreFilter;
//do something here with the filtered inputs, PreFilterOutLeft and PreFilterOutRight
until False;
end.
Re: lowpass filter
That doesn't realy help.
It must be filteres hi and low spikes.
That doesn't really work with this solution
It must be filteres hi and low spikes.
That doesn't really work with this solution
Re: lowpass filter
The filter should filter out spikes
3.65, 3.55. 3.57, 3.43,3.60, 3,65, 3,64
Such spikes are too much for an averaging filter.
If necessary, I would measure 16 values, throw out the lowest and the highest value, and then form the average value there.
3.65, 3.55. 3.57, 3.43,3.60, 3,65, 3,64
Such spikes are too much for an averaging filter.
If necessary, I would measure 16 values, throw out the lowest and the highest value, and then form the average value there.
Re: lowpass filter
Yes, low pass filter does not remove spikes - it averages them out. If you want to remove this kind of disturbance, you need another method and it has to be done before the low-pass filter. One may, for example, always remove highest and lowest readings from a set of measurements of assumed size which does not guarantee that no spikes will pass. Any other method, like setting a window around acquired average requires some prior knowledge about the signal to be measured (like its allowed magnitude of changes, its maximum frequency) and expected speed of response of the algorithm to sudden signal changes.