Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Exponential Filter for sound sampling
- *
- * This allows slower decay of the signal. Best used with floating point arithmetic.
- *
- * Steps are:
- *
- * Get a 10 bit sample
- * Remove background noise
- * Adjust gain
- * An 8 bit filter
- * Convert 8 bit filter to floating point
- * Apply filter to adjusted gain
- * You get resultant signal.
- *
- */
- float micLev = 0.0;
- int sample;
- int sampleAdj;
- // Floating exponential filter
- float expAdjF;
- uint8_t weighting = 128;
- float weightingF = 0.02; // Need to translate 0-255 to a reasonable floating point range.
- uint8_t soundSquelch = 5; // Adjustable squelch.
- uint8_t sampleGain = 10; // Adjustable gain.
- #define MIC_PIN A5
- void setup() {
- Serial.begin(115200);
- analogReference(EXTERNAL);
- }
- void loop() {
- long micIn;
- micIn = analogRead(MIC_PIN);
- micLev = ((micLev * 31) + micIn) / 32; // Smooth it out over the last 32 samples for automatic centering.
- micIn -= micLev; // Let's center it to 0 now
- micIn = abs(micIn); // And get the absolute value of each sample
- // Translate uint8_t weighting to a floating point value.
- weightingF = (float)(weighting+1)/500.0; // Limits should be .002 to .512
- // Squelching and smoothing of an analog signal for volume reactive routines.
- //Using a ternary operator, the resultant sample is either 0 or it's a bit smoothed out with the last sample.
- sample = (micIn <= soundSquelch) ? 0 : (sample * 3 + micIn) / 4;
- // Adjust the gain.
- sampleAdj = sample * sampleGain / 40 + sample / 16;
- // Floating exponential filter weighting.
- expAdjF = (weightingF * (float)sampleAdj + (1.0-weightingF) * expAdjF);
- Serial.print("0:"); Serial.print(0);
- // Serial.print(",micIn:"); Serial.print(micIn);
- // Serial.print(",sample:"); Serial.print(sample);
- Serial.print(",sampleAdj:"); Serial.print(sampleAdj);
- Serial.print(",expadjF:"); Serial.print(expAdjF);
- Serial.print(",weightingF:"); Serial.print(weightingF,3);
- // Serial.print(",Max:"); Serial.print(255);
- // Serial.print(",Min:"); Serial.print(-160);
- Serial.println(" ");
- /* Serial.print("sampleAdj:\t"); Serial.print(sampleAdj);
- Serial.print("Max:500");Serial.print(512);
- Serial.print("Min:0");Serial.println(" ");
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement