Generating 20 frequencies mixed signal

Status
Not open for further replies.
Hello.
I am since some time creating a small project to have a small generator of up to 20 frequencies. But one thing is bothering me.
I use Teency 3.2 and the Audio board rev. C and have some doubts on the expected noise of the generated frequency.
I have reduced the code to the bare minimum, so that maybe someone can cross-check my result.
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioSynthWaveform       waveform1;      //xy=64,321
AudioSynthWaveform       waveform2;      //xy=64,366
AudioSynthWaveform       waveform3;      //xy=64,405
AudioSynthWaveform       waveform4;      //xy=64,452
AudioSynthWaveform       waveform5;      //xy=64,511
AudioSynthWaveform       waveform6;      //xy=64,553
AudioSynthWaveform       waveform7;      //xy=64,596
AudioSynthWaveform       waveform8;      //xy=64,640
AudioSynthWaveform       waveform9;      //xy=64,693
AudioSynthWaveform       waveform11;     //xy=67.5,791
AudioSynthWaveform       waveform10;     //xy=68,743
AudioSynthWaveform       waveform12;     //xy=68,840
AudioSynthWaveform       waveform13;     //xy=68,889
AudioSynthWaveform       waveform14;     //xy=68,936
AudioSynthWaveform       waveform15;     //xy=68,983
AudioSynthWaveform       waveform16;     //xy=68,1029
AudioSynthWaveform       waveform17;     //xy=68,1073
AudioSynthWaveform       waveform18;     //xy=68,1116
AudioSynthWaveform       waveform19;     //xy=68,1159
AudioSynthWaveform       waveform20;     //xy=68,1207
AudioMixer4              mixer5;         //xy=351,1123
AudioMixer4              mixer3;         //xy=355,738
AudioMixer4              mixer1;         //xy=357,388
AudioMixer4              mixer4;         //xy=355,942
AudioMixer4              mixer2;         //xy=359,562
AudioMixer4              mixer6;         //xy=574,608
AudioMixer4              mixer7;         //xy=577,889
AudioMixer4              mixer8;         //xy=740,761
AudioOutputUSB           usb1;           //xy=909,653
AudioOutputI2S           i2s1;           //xy=916,761

AudioConnection          patchCord1(waveform1, 0, mixer1, 0);
AudioConnection          patchCord2(waveform2, 0, mixer1, 1);
AudioConnection          patchCord3(waveform3, 0, mixer1, 2);
AudioConnection          patchCord4(waveform4, 0, mixer1, 3);
AudioConnection          patchCord5(waveform5, 0, mixer2, 0);
AudioConnection          patchCord6(waveform6, 0, mixer2, 1);
AudioConnection          patchCord7(waveform7, 0, mixer2, 2);
AudioConnection          patchCord8(waveform8, 0, mixer2, 3);
AudioConnection          patchCord9(waveform9, 0, mixer3, 0);
AudioConnection          patchCord10(waveform11, 0, mixer3, 2);
AudioConnection          patchCord11(waveform10, 0, mixer3, 1);
AudioConnection          patchCord12(waveform12, 0, mixer3, 3);
AudioConnection          patchCord13(waveform13, 0, mixer4, 0);
AudioConnection          patchCord14(waveform14, 0, mixer4, 1);
AudioConnection          patchCord15(waveform15, 0, mixer4, 2);
AudioConnection          patchCord16(waveform16, 0, mixer4, 3);
AudioConnection          patchCord17(waveform17, 0, mixer5, 0);
AudioConnection          patchCord18(waveform18, 0, mixer5, 1);
AudioConnection          patchCord19(waveform19, 0, mixer5, 2);
AudioConnection          patchCord20(waveform20, 0, mixer5, 3);
AudioConnection          patchCord21(mixer5, 0, mixer7, 3);
AudioConnection          patchCord22(mixer3, 0, mixer6, 2);
AudioConnection          patchCord23(mixer1, 0, mixer6, 0);
AudioConnection          patchCord24(mixer4, 0, mixer6, 3);
AudioConnection          patchCord25(mixer2, 0, mixer6, 1);
AudioConnection          patchCord26(mixer6, 0, mixer8, 0);
AudioConnection          patchCord27(mixer7, 0, mixer8, 3);
AudioConnection          patchCord28(mixer8, 0, i2s1, 0);
AudioConnection          patchCord29(mixer8, 0, i2s1, 1);
AudioConnection          patchCord30(mixer8, 0, usb1, 0);
AudioConnection          patchCord31(mixer8, 0, usb1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=614,187

const int N_waves = 20;
float base_freq = 855.0;
float freq_list[N_waves];
float volume_main = 0.8;
float volume_dig = 0.0;

AudioSynthWaveform * Waves[N_waves] = 
{
  &waveform1, &waveform2, &waveform3, &waveform4, &waveform5, &waveform6, &waveform7, &waveform8, &waveform9, &waveform10,
  &waveform11, &waveform12, &waveform13, &waveform14, &waveform15, &waveform16, &waveform17, &waveform18, &waveform19, &waveform20
}; 

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('A');
    delay(300);
  }
}

void setup() {
  Serial.begin(9600);

  while (!Serial && millis() < 2500) ; // wait

//  establishContact();
  
  Serial.println("Ready!");

  AudioMemory(30);
  
  analogReference(INTERNAL);   // WHY
  
  mixer1.gain(0,0.8);
  mixer1.gain(1,0.8);
  mixer1.gain(2,0.8);
  mixer1.gain(3,0.8);

  mixer2.gain(0,0.8);
  mixer2.gain(1,0.8);
  mixer2.gain(2,0.8);
  mixer2.gain(3,0.8);

  mixer3.gain(0,0.8);
  mixer3.gain(1,0.8);
  mixer3.gain(2,0.8);
  mixer3.gain(3,0.8);

  mixer4.gain(0,0.8);
  mixer4.gain(1,0.8);
  mixer4.gain(2,0.8);
  mixer4.gain(3,0.8);

  mixer5.gain(0,0.8);
  mixer5.gain(1,0.8);
  mixer5.gain(2,0.8);
  mixer5.gain(3,0.8);

  mixer6.gain(0,0.8);
  mixer6.gain(1,0.8);
  mixer6.gain(2,0.8);
  mixer6.gain(3,0.8);

  mixer7.gain(0,0.0);
  mixer7.gain(1,0.8);
  mixer7.gain(2,0.0);
  mixer7.gain(3,0.8);

  mixer8.gain(0,0.8);
  mixer8.gain(1,0.0);
  mixer8.gain(2,0.0);
  mixer8.gain(3,0.8);
  
  sgtl5000_1.enable();
  sgtl5000_1.volume(volume_main);     // set the main volume...
  sgtl5000_1.adcHighPassFilterDisable();

  delay(1000);

  for(int i=0; i < N_waves; ++i)
  {
    freq_list[i] = base_freq + base_freq * i;
    Serial.println(freq_list[i]);
  }

  AudioNoInterrupts();
  
  for(int i=0; i < N_waves; ++i)
  {
    Waves[i]-> frequency(freq_list[i]);
    Waves[i]-> amplitude( volume_main/N_waves );
    Serial.println(volume_main/N_waves);
    Waves[i]-> phase(0.0);
  }

  AudioInterrupts();

  for(int i=0; i < N_waves; ++i)
  {
    Waves[i]-> begin(WAVEFORM_SINE);
  }
  
}

void loop() {

 int inByte = 0;
  
 if (Serial.available() > 0)
  {
    inByte = Serial.read() - 48;
    Serial.print(inByte);

    if(inByte >= 0 && inByte <= 19)
    {
      AudioNoInterrupts();
      Waves[inByte]-> amplitude(0.0);
      AudioInterrupts(); 
      
    }
    else if(inByte == 20 || inByte == 21 || inByte == 22 || inByte == 23)
    {
      if(inByte == 20)
      {
        AudioNoInterrupts();
        sgtl5000_1.volume(0.0); 
        AudioInterrupts(); 
      }
      else if(inByte == 71)
      {
        AudioNoInterrupts();
        sgtl5000_1.volume(0.3); 
        AudioInterrupts(); 
      }
      else if(inByte == 53)
      {
        AudioNoInterrupts();
        sgtl5000_1.volume(0.6); 
        AudioInterrupts(); 
      }
      else if(inByte == 66)
      {
        AudioNoInterrupts();
        sgtl5000_1.volume(0.9); 
        AudioInterrupts(); 
      }
      
    }
    else
    {
      Serial.println("Wrong argument");
    }
    
    delay(300);

  }

}

The resultign spectrum on the USB audio looks like:
20_tones_FFT.jpg

Does it looks like one would expect?
I was expecting much sharper peaks as this method (USB audio) as far as I got is using the digital transmission with no EM noise or speaker distortions...
Thanks for any help or advice on how to generate the cleanest possible sound.
 
I ran your sketch on a T3.6 with Rev C board. Using a Win10 64 Pro PC with Arduino 1.8.12 and TD 1.52 B1.
I generated the attached spectrum using Goldwave which reads the audio directly from the Teensy USB.
tones_spectrum.jpg
When Goldwave first starts recording, the peaks widen briefly (which shows as the vertical lines in the upper spectrogram) - maybe this is a buffering problem between the Teensy and PC?. But then it settles down and produces a solid, unvarying spectrum.

Pete
 
Thanks for the investigation. I have tried to install the Goldwave but (probably due to bad configuration) still see a bumpy spectrum, like if there are constant transitions between the one You show and the one in the initial post.
How long have you registered until it did settle to the nice spectrum? Because on 20s registration is like it sometime starts ok but then I see again the noisy picture.
I use TD 1.51, Win 10 64Bit and Arduino 1.8.12.
20_tones_FFT_Goldwave.jpg
 
It seems that I see a very drastic difference if I change in the Device property the the volume.
at 0db
20_tones_FFT_Goldwave_0db.jpg
at -30db
20_tones_FFT_Goldwave_30db.jpg

Still is strange the bumping spectrum.
 
Status
Not open for further replies.
Back
Top