Producing a varying frequency sine wave on DAC output of the Teensy LC

Status
Not open for further replies.

edis1122

Member
Hello,
So from previous threads, I have found a method to produce a sine wave on the DAC output (A12) of the Teensy LC board. I have attached the code below that I am using and I was wondering if anybody could help me in changing the code. The code output requires to have a constant amplitude throughout however the frequency of the sine wave must begin at 5Hz and increase up to 25Hz. The frequency has to increase by 1Hz every 3 seconds summing to a change of 20 Hz (5 to 25) in 1 minute. I am a beginner at this so simpler explanations would be appreciated ;)
 

Attachments

  • qtrOffsettables.c
    2.2 KB · Views: 47
  • KO7MDDSCodeIntegerQuarterTeensyLC_V2.ino
    6.9 KB · Views: 44
The sine wave I am generating is using the Look-Up table method. In the post it uses just the formula. Would the signal be just as accurate using a formula instead of a look up table. And, even if it is, to change the frequency of the signal using look up tables would you have to change the contents do the table?
 
I am a beginner at this so simpler explanations would be appreciated ;)

The audio library can do this for you, and it's very simple to use.

Code:
#include <Audio.h>

AudioSynthWaveformSine   sine1;          //xy=139,82
AudioOutputAnalog        dac1;           //xy=322,90
AudioConnection          patchCord1(sine1, dac1);

void setup() {
  AudioMemory(2);
  sine1.frequency(2.30); // 2.3 Hz
  sine1.amplitude(0.9);
}

void loop() {
  Serial.println(AudioProcessorUsage());
  delay(1000);
}

file.png
 
The sine wave I am generating is using the Look-Up table method. .... And, even if it is, to change the frequency of the signal using look up tables would you have to change the contents do the table?

Internally the audio library uses this table lookup approach. The table is a fixed size. The frequency is changed altering how far each output advances within the table. The position within the table is kept with extra resolution, much more than the step size of the table's points. Each lookup fetches the nearest 2 points from the table and the actual output is computed by linear interpolation between those 2 table entries. If you really want to dive into the code, it's all on github and within the {Arduino}/hardware/teensy/cores/avr/libraries/Audio folder on your PC.

The audio library also has a high resolution sine wave feature using a formula approach with 11th order Taylor series approximation. But it depends on the Cortex M4/M7 DSP extension instructions, so it won't run on the Cortex M0+ processor in Teensy LC.
 
The audio library also has a high resolution sine wave feature using a formula approach with 11th order Taylor series approximation. But it depends on the Cortex M4/M7 DSP extension instructions, so it won't run on the Cortex M0+ processor in Teensy LC.

Thank you for the explanation it was quite helpful but I didn't understand a few things. Is the above quote saying that the audio library cannot be used with Teensy LC module? Also, when using analogWrite(x,y) what is the y parameter of the function? As in, it is the variable to be written to the output pin x but is it sine1, dac1 or patchCord1 or even something else from the code written above. I assumed it is dac1 however when pairing the code with the Teensy LC it is not compiling. Maybe my code is wrong, I'll attach just in case.

View attachment Test_For_Sine_Wave.ino
 
Is the above quote saying that the audio library cannot be used with Teensy LC module?

You can use the audio library on Teensy LC. The waveform you see on #4 was measured on the DAC pin of Teensy LC while running that code in msg #4.

But not all audio library features work on Teensy LC. AudioSynthWaveformSine works, but AudioSynthWaveformSineHires does not.


Also, when using analogWrite(x,y) what is the y parameter of the function?

analogWrite() isn't part of the audio library. It directly controls the DAC pin, and PWM pins. The first parameter is which pin to control.

If you try to create a waveform with analogWrite(), you must craft code to update the pin at very high speed, with very consistent timing, and with the proper sequence of number to achieve the waveform you want. analogWrite() directly controls the pin. Using it to do something elaborate like synthesizing a waveform is on you. Doable, but not easy, especially to get the update timing precise & consistent.

That's why I recommended you use the audio library. I even wrote that little test program on msg #4 and checked that it really does produce a sine wave on Teensy LC's DAC pin. Have you tried running the msg #4 code on your Teensy LC yet?


Maybe my code is wrong, I'll attach just in case.

Yup, that's wrong.

Use either the audio library, which does all the work for you to rapidly update the pin, or use analogWrite where you have to do all that work, but don't mix them in the same program.

When using the audio library, you control the waveform using sine1.frequency() and sine1.amplitude(). Then the audio library takes that info and automatically keeps updating the pin for you.

Remember, loop() runs over and over at high speed. If you want to increase the frequency over time, you'll need to use sine1.frequency() and either delay() or other timing functions, so you tell the audio library to create a new frequency when the time is right. Without any delay, you'll end up telling it to change before anything has had any time to actually happen.
 
Thank you again for another amazing explanation. The tester code you have given me has worked and I've also managed to write the code for the sweeping sine wave using a state machine approach. I just have one last question; what does the parameter x in AudioMemory(x) specify? Thank you
 
AudioMemory(x) just allocates a block of x buffers to the audio system and then initializes them. It is just a macro which is defined like this (in AudioStream.h):
Code:
#define AudioMemory(num) ({ \
	static DMAMEM audio_block_t data[num]; \
	AudioStream::initialize_memory(data, num); \
})

Pete
 
Status
Not open for further replies.
Back
Top