lowpass filter

General discussion on mikroPascal PRO for PIC.
Post Reply
Author
Message
corado
Posts: 399
Joined: 28 Mar 2009 11:03

lowpass filter

#1 Post by corado » 18 Aug 2021 10:17

Does anybody have working code(i'm Beginner) for an lowpass filter in Pascal?
I need this for ADC Measurement

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: lowpass filter

#2 Post by VCC » 29 Aug 2021 18:50

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.

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.
HTH :D

corado
Posts: 399
Joined: 28 Mar 2009 11:03

Re: lowpass filter

#3 Post by corado » 30 Aug 2021 09:51

That doesn't realy help.
It must be filteres hi and low spikes.
That doesn't really work with this solution

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: lowpass filter

#4 Post by VCC » 30 Aug 2021 11:21

corado wrote:
30 Aug 2021 09:51
It must be filteres hi and low spikes.
I didn't get this. Please use Google Translate. And also, please provide more details about what you want to do and why the posted code is not suitable. For a more complex design, maybe somebody else can help you.
:)

corado
Posts: 399
Joined: 28 Mar 2009 11:03

Re: lowpass filter

#5 Post by corado » 30 Aug 2021 11:41

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.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: lowpass filter

#6 Post by janni » 30 Aug 2021 12:02

corado wrote:
30 Aug 2021 09:51
That doesn't realy help.
It must be filteres hi and low spikes.
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.

Post Reply

Return to “mikroPascal PRO for PIC General”