Metal detector dc42 improvment

What are you developing?
User avatar
ahull
Posts: 1727
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Metal detector dc42 improvment

Post by ahull » Sat Feb 10, 2018 11:30 am

Related (sort of) -> https://hackaday.com/2018/02/10/all-the ... id-to-ask/



Related in the sense that this series of videos tells you in a relatively digestible format *how* removal of a particular frequency from a bunch of frequencies actually works.

For those of you who consider your maths is a little rusty, you might like to go through the "unit circle" stuff first in the following series of videos to get a grip of the underlying trig concepts.



Also a read through of this related thread... -> viewtopic.php?t=1145&start=30#p14677
... and the video here ...





might be worth your time too.
- Andy Hull -

User avatar
Pito
Posts: 1738
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Metal detector dc42 improvment

Post by Pito » Sat Feb 10, 2018 1:53 pm

60Hz bandstop:
with an FIR filter you can get for example this (click on "Source Code", select ie. Number format: Integer and 32bit int precision and copy/paste the source) - LINK is in the picture below:
60Hz bandstop_minus 90db.JPG
60Hz bandstop_minus 90db.JPG (85.54 KiB) Viewed 78 times
.
Look at this my source how the FIR filter source shall be integrated into your source - look how you have to input data into the FIR and how you get outputs from the FIR (ie in a 400Hz ADC sampling loop to get above results):
viewtopic.php?f=3&t=1453&p=19827&hilit=pito+fir#p18760
Last edited by Pito on Sat Feb 10, 2018 2:39 pm, edited 3 times in total.
Pukao Hats Cleaning Services Ltd.

User avatar
ahull
Posts: 1727
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Metal detector dc42 improvment

Post by ahull » Sat Feb 10, 2018 2:13 pm

Very neat. I like it. (link to above mentioned site -> here <- )
- Andy Hull -

User avatar
Pito
Posts: 1738
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Metal detector dc42 improvment

Post by Pito » Sat Feb 10, 2018 3:22 pm

Code: Select all

..
#include "SampleFilter.c"
..
int32_t signal;  // the actual ADC signal sample
int32_t fsig;  // internal FIR filter variable

setup() {
..
SampleFilter_init(&fsig);  // you have to init the FIR filter
..
}

HERE IS A 400Hz SAMPLING LOOP >>>>>

    signal = readAnalog(PA4);  // get 12bit signal sample

    SampleFilter_put(&fsig, signal);  // pass the signal into the FIR filter
    signal = SampleFilter_get(&fsig);  // read the signal from the FIR filter
..
DO SOMETHING WITH THE FILTERED signal
..
<<<<<  END OF THE 400Hz SAMPLING LOOP
You may chain the filters, for example:

Code: Select all

ADC-signal -> FIR1 -> FIR2 -> filtered-signal
ie. filter1 is a notch filter and the filter2 is a low pass (both filters must have the same sampling frequency):

Code: Select all

..
#include "SampleFilter1.c"
#include "SampleFilter2.c"
..
int32_t signal;  // the actual ADC signal sample
int32_t fsig1;  // internal FIR filter variable
int32_t fsig2;  // internal FIR filter variable

setup() {
..
SampleFilter_init1(&fsig1);  // you have to init the FIR filter
SampleFilter_init2(&fsig2);  // you have to init the FIR filter
..
}

HERE IS A 400Hz SAMPLING LOOP >>>>>

    signal = readAnalog(PA4);  // get 12bit signal sample

    SampleFilter_put1(&fsig1, signal);  // pass the signal into the FIR1 filter
    signal = SampleFilter_get1(&fsig1);  // read the signal from the FIR1 filter
    
    SampleFilter_put2(&fsig2, signal);  // pass the signal into the FIR2 filter
    signal = SampleFilter_get2(&fsig2);  // read the signal from the FIR2 filter
..
DO SOMETHING WITH THE FILTERED signal
..
<<<<<  END OF THE 400Hz SAMPLING LOOP
The best results (noise, artifacts) you may get with the floating point FIRs, but that would not work with too many FIR taps and stm32F10x easily (floating point math too slow). It may work fine with F40x and FPU on, however..
Pukao Hats Cleaning Services Ltd.

ted
Posts: 62
Joined: Sun Jul 16, 2017 9:57 pm

Re: Metal detector dc42 improvment

Post by ted » Sat Feb 10, 2018 4:02 pm

Thank You for your support, I am reading all info, I can generate source code for filters but I don't know yet how to implement them, I mean add the functions for input and output pins.
Here is the code which was working before but when I saved it and opened again the PWM on pin PB1 is not there any more. In this program is also notch filter.

Code: Select all


#include <FIRFilter.h>
#include <IIRFilter.h>

const float ECG_samplefreq  = 360;
const uint8_t ECG_pin = PB0;
const int16_t DC_offset = 511;

//const uint8_t ECG_pin = PB1; //output pin
int outputPin = PB1; //output pin
//int outputPin = PA6; //output pin

// 50 Hz notch
//const float b_notch[] = { 1.39972748302835,  -1.79945496605670, 1.39972748302835 };

/*
 
 // 35 Hz Butterworth low-pass
const float b_lp[] = { 0.00113722762905776, 0.00568613814528881, 0.0113722762905776,  0.0113722762905776,  0.00568613814528881, 0.00113722762905776 };
const float a_lp[] = { 1, -3.03124451613593, 3.92924380774061,  -2.65660499035499, 0.928185738776705, -0.133188755896548 };
*/

const float a_coefficients[] =  {1, 3.610968072739773, 5.096060175734044, 3.317095275389235, 0.844114606367055};
const float b_coefficients[] = {0.003306153133358, 0, -0.006612306266715, 0, 0.003306153133358};
/*
// 0.3 Hz high-pass
const float b_hp[] = { 1, -1 };
const float a_hp[] = { 1, -0.995 };

FIRFilter notch(b_notch);
//IIRFilter lp(b_lp, a_lp);
IIRFilter hp(b_hp, a_hp);
*/
void setup() {
  Serial.begin(115200);
}

unsigned long ECG_prevmicros = micros();

void loop() {
  const static unsigned long ECG_interval  = round(1e6 / ECG_samplefreq);
  
  if (!Serial) {
    ECG_prevmicros = micros();
  } else if (micros() - ECG_prevmicros >= ECG_interval) {
    measureECGAndSend();
    ECG_prevmicros += ECG_interval;
  }
}

void measureECGAndSend() {
  int16_t value = analogRead(ECG_pin);
  float filtered = //notch.filter(
                      //lp.filter(
//                      hp.filter(value - DC_offset));
  value = round((filtered) + DC_offset);
  Serial.println(value);
   
    //Serial.print(value); //  PWM on PB1
  value = map(value, 0, 1023, 0, 255);
value = constrain(value, 0, 255);
analogWrite(outputPin, value);     //  output pin
}



ted
Posts: 62
Joined: Sun Jul 16, 2017 9:57 pm

Re: Metal detector dc42 improvment

Post by ted » Sat Feb 10, 2018 8:05 pm

Thanks Pito
I have read everything including the link, but with my programming knowledge I am not able to put everything together.

Post Reply