View Full Version : Basic Signal Cancellation/Comparison Program?

04-23-2016, 10:33 PM

This is my first Teensy program, and, though I can't yet test it on my Teensy (a component from my setup broke, and the replacement is still shipping), I'm sure I'm doing something obviously wrong, and I have a few questions.

I want to take two audio signals (one from each ADC), low-pass filter them, scale them so that they're of equal amplitude, then invert one and add it to the other. By going through and writing every bin-value below a threshold to zero, I was hoping this would allow me to "cancel" the noise in one signal with the inverted noise from the other (a control).

For one, how do I determine how much audio memory to allocate? Also, does this "scaling" I'm doing work as scaling, or does it simply mess up the data? What sort of other errors jump out as obvious to experienced programmers?

Thanks so much!

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputAnalog adc1; //xy=82,93
AudioInputAnalog adc2; //xy=82,143
AudioAnalyzePeak peak1; //xy=243,26
AudioFilterStateVariable filter1; //xy=249,97
AudioFilterStateVariable filter2; //xy=250,163
AudioAnalyzePeak peak2; //xy=252,230
AudioAnalyzeFFT1024 fft1024_1; //xy=433,86
AudioAnalyzeFFT1024 fft1024_2; //xy=437,167
AudioConnection patchCord1(adc1, peak1);
AudioConnection patchCord2(adc1, 0, filter1, 0);
AudioConnection patchCord3(adc2, peak2);
AudioConnection patchCord4(adc2, 0, filter2, 0);
AudioConnection patchCord5(filter1, 0, fft1024_1, 0);
AudioConnection patchCord6(filter2, 0, fft1024_2, 0);
// GUItool: end automatically generated code

double scale_factor_1;

void setup() {

AudioMemory(12); // how do I choose how much audio memory?

fft1024_1.WindowFunction(AudioWindowHanning1024); // which window?

scale_factor_1 = 0;



void loop() {

if (peak1.available() && peak2.available()) {
scale_factor_1 *= (peak1.read() / peak2.read()); // ratio?
} // peak, or peak-to-peak? I'm thinking the second...

if (fft1024_1.available() && fft1024_2.available()) { // if these have 500+ bins why tf does the ex code only cycle through 40???
for(int i = 0; i <= 511; i++){
if ( abs( fft1024_1.read(i) * scale_factor_1 + fft1024_2.read(i) ) > 0.1 ) { // replace .01 with "SIGNIFICANT" defined static var.
// implementation
} // Is it bad/incorrect to scale frequency bins in this manner?

Frank B
04-24-2016, 10:11 PM
Perhaps this is of some use for you ? It does the part "then invert one and add it to the other."


Edit: Or, perhaps better - the mid-side effect.

You could apply the filters before that.
Not sure what you intend to do with the FFTs...

04-25-2016, 02:00 AM
Hey, that's really cool! Thanks!

Basically I wanted to, after adding the one inverted signal to the normal one, check each frequency bin and see if there's any significant "difference" there between the two signals -- i.e., if the sum of them is some significant amount greater than 0, then one of them is detecting frequencies that the other isn't.