Filter library test

Probably, your sampling rate isn't defined at 5 kHz, but vary above the spec.

Yes, you are right! :slight_smile:
I have a digital oscilloscope and did the measurement how as suggested by you. Here the sketch:

// Arduino Signal Filtering Test
// Turn a LED on if filter amplitude reaches an arbitrary threshold.
// 2014  

// Arduino Signal Filtering Library
// Copyright 2012-2013 Jeroen Doggen (jeroendoggen@gmail.com)
//http://jeroendoggen.github.io/Arduino-signal-filtering-library/
//http://www.schwietering.com/jayduino/filtuino/


#include <SignalFilter.h>  // Filter Library
SignalFilter Filter;

int Pin8 = 8;   // for sampling measurement 

const int ledPin = 13;        // pin that the LED is attached to
const int threshold = 512;    // an arbitrary threshold level that's in the range of the analog input

/* // Variable for time measurement
unsigned long time_start;    // variable for time measurement start
unsigned long time_sampl;    // variable for time measurement sample
int incomingByte = 0;        // sets incoming Byte to 0
*/

int data1;     // variable for Analog In 
int data2;     // variable for filter


void setup()
{
  pinMode(Pin8, OUTPUT);        // for sampling measurement 
  pinMode(ledPin, OUTPUT);     // initialize the LED pin as an output:
 // Serial.begin(9600);        // used only printing (debugging)
  Filter.begin();
  Filter.setFilter('c');       // filter settings: c=Cebycheff  b= Bessel
  Filter.setOrder(2);          // filter settings: order 1 or 2     
}

/* 
      Filter characteristics are stored in: “SignalFilter.cpp”.
      Current filter settings and code: for “c” “2”

/// runChebyshev: Band Pass 220Hz; 1. Order; lower corner =215Hz; upper corner 225Hz; sampling=5000Hz; long type 8bit; enabled "-1 bit saturation";
			_v[0] = _v[1];
			_v[1] = _v[2];
			long tmp = ((((data * 3360365L) >>  8)	//= (   6.2591678898e-3 * x)
				+ ((_v[0] * -4141801L) >> 1)	//+( -0.9874824279*v[0])
				+ (_v[1] * 4009861L)	//+(  1.9120508347*v[1])
				)+1048576) >> 21; // round and downshift fixed point /2097152

			_v[2]= (short)tmp;
			return (short)((
				 (_v[2] - _v[0]))); // 2^
*/


void loop()
{
 // Read Analog Input... you can connect accelerometer, gyro, temperature sensor, microphone etc to the channel
  
  data1 = analogRead(0)-512;      //Read Analog channel (value of microphone) and substract DC-Offset from Microphone "BOB-09964"
  data2 = Filter.run(data1);      //Read the filtered signal 
  
/*
http://arduino.cc/en/Tutorial/IfStatement
the variable called data1 is used to store the data collected from a Analog Pin A0. 
This data is then compared to a threshold value. 
If the the value is found to be above the set threshold the LED connected to digital pin 13 is turned on. 
If analogValue is found to be < threshold, the LED remains off. */


  // if the filter value is high enough, turn on the LED:
  if (data2 > threshold) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin,LOW);
  }
   
 
    digitalWrite(Pin8, HIGH);  // PIN Output High Sample Measurement
    digitalWrite(Pin8, LOW);   // PIN Output Low Sample Measurement 
    
    
   /* // time measurement for debugging:
 
   time_start = micros();               //time measurement 
   data1 = analogRead(0);              // for Analog Pin A0
   data2 = Filter.run(data1);          // for filter calculation
   time_sampl = micros() - time_start; //time measurement math
  
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if (incomingByte == 'm') {         // enter and sent "m" in serial monitor window and read time in microsec for Filter.run(data1) and analog read
    Serial.print("Time Sample = "); 
    Serial.println(time_sampl, DEC);
        }
      } */
}

It gives me a frequency between 6,350 KHz and 6,579KHz (attachment).
On my other sketch (filter and sine wave) the sample rate is about 5,9KHz
Are there possibilities to control the sampling frequency?