Go Down

### Topic: Bandlimited waveform generation (Read 3570 times)previous topic - next topic

#### pyrohaz

##### May 13, 2013, 04:50 am
Hey guys,

I know that when you try and reproduce waveforms like square waves and sawtooth waves, they include harmonics that extend infinitely (theoretically). When producing these digitally, these harmonics are above the nyquist frequency and aliase back into the audio frequency sometimes.

One way that I can think of to avoid this is by producing the square wave through additive synthesis (sin(x)+sin(3x)/3+sin(5x)/5 etc) though this requires a ton of processor power, especially on an arduino where the sine function can take ages to compute.

Obviously, producing a square wave with infinite harmonics could be done through:
Code: [Select]
`for(n = 0; n<2048; n++){if (n>pulsewidth){        out[n] = 32768;    }else{        out[n] = -32768;}}`

Where pulse width is between 0 and 2048 (both 0 and 2048 being DC). Since the above method can produce a square wave with a variable pulse width between 0 and 2048, and producing this pulse width using fourier series would be a bit of a ball ache, could I band limit this by putting it through a 'leaky integrator' style low pass filter? Changing the code from the above to:

Code: [Select]
`for(n = 0; n<2048; n++){if (n>pulsewidth){        outv = 32768;    }else{        outv = -32768;}outp = outp + 500*(outv-outp)/1024;out[n] = outp}`

Will doing the above produce a bandlimited square wave? Where the level of band limiting is determined by the "frequency" variable, or in the above situation, the 500?

Cheers, Harris

#1
##### May 13, 2013, 07:34 am
328P only puts out square waves, with the harmonics generated by the sharpness of voltage change at the low/high high/low transition.
Slowing that transition by use for Low pass filter reduces the harmonics.
I don't know what base frequency your code will generate; square waves can be generated in multiple ways with duty cycle changing from 50/50 to whatever.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### DuaneB

#2
##### May 13, 2013, 10:01 am
If your already using a wavetable for your sinewave, why not use it in the calculation as well, thats going to be a lot faster.

If you really wanted to generate a band limited squarewave, you could do the calculation in loop and flag your ISR when the wavetable is ready this way you get what you want reasonably quickly without messing up the audio generation.

Alternativley I have seen that wave shaping can acheive the same result with less computation however I have not yet been able to understand this approach

Duane B

rcarduino.blogspot.com
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

#### Grumpy_Mike

#3
##### May 13, 2013, 10:11 am
Quote
When producing these digitally, these harmonics are above the nyquist frequency and aliase back into the audio frequency sometimes.

No they don't.
Aliasing only occours when you sample a signal not when you generate it. This is coped with by using a low pass filter befor the A/D. I don't see why you are worrying about this.

#### pyrohaz

#4
##### May 13, 2013, 08:30 pm
Brilliant! If I don't have to worry about it then i'm happy not doing so!

Thanks,
Harris

#### Nantonos

#5
##### May 14, 2013, 12:42 pm

Quote
When producing these digitally, these harmonics are above the nyquist frequency and aliase back into the audio frequency sometimes.

No they don't.
Aliasing only occours when you sample a signal not when you generate it.

Sorry, that is incorrect.

Its perfectly possible to create an aliased signal using DDS to create a waveform (square, sine, or whatever does not matter) and its well known how to avoid this by creating bandlimited signals. The typical way it is done in practice is to use multiple wavetables, such as one per octave, with each table containing progressively less harmonics. Although this can produce audible artifacts on rapid frequency sweeps.

A useful introduction is the paper by Stilson & Smith Alias-Free Digital Synthesis of Classic Analog Waveforms
https://ccrma.stanford.edu/~stilti/papers/blit.pdf

These lecture notes on bandlimited synthesis are also helpful
http://www.music.mcgill.ca/~gary/307/week5/bandlimited.html

This discussion includes sample code
http://slack.net/~ant/bl-synth/

All of these assume that the analog DDS waveform is being produced by a DAC, rather than messing around with optimistic pulse width modulation hackery.

#### Grumpy_Mike

#6
##### May 14, 2013, 06:11 pm
Quote
Sorry, that is incorrect.

No I don't think you understand those papers.
How were those spectrums obtained? By digitising the signal.

Yes you can generate a band width limited signal but unless you are going to send it through an other digitiser then there is no point.
If that assumption were true then you would get aliasing from a simple square wave oscillator which is clearly nonsense.

It says:-
Quote
Any analog signal with a discontinuity in the wave- form (such as pulse train or sawtooth) or in the waveform slope (such as triangle wave) must be bandlimited to less than half the sampling rate before sampling to obtain a corresponding discrete-time signal. Simple methods of generating these waveforms digitally contain aliasing due to having to round off the discontinuity time to the nearest available sampling instant. The signals primarily addressed here are the impulse train, rectangular pulse, and sawtooth waveforms. Because the latter two signals can be derived from the first by integration, only the algorithm for the impulse train is developed in detail.

My red words. This is a signal that is about to be digitised not one that has been generated and is being listened to. Which is what I took the OP to be asking about.

Quote
All of these assume that the analog DDS waveform is being produced by a DAC, rather than messing around with optimistic pulse width modulation hackery.

That makes no difference to what we are talking about here, it is just extra quantization noise outside the band we are interested in.

#### Nantonos

#7
##### May 14, 2013, 09:06 pm

Quote
Sorry, that is incorrect.

No I don't think you understand those papers.
How were those spectrums obtained? By digitising the signal.

If by digitising you mean starting with an analog signal and then sampling it (the usual meaning) then no. There is no analog to digital conversion in this process. The digital signal produced is created directly in the digital domain. hence the name Direct Digital Synthesis.

It says:-
Quote
Any analog signal with a discontinuity in the wave- form (such as pulse train or sawtooth) or in the waveform slope (such as triangle wave) must be bandlimited to less than half the sampling rate before sampling to obtain a corresponding discrete-time signal. Simple methods of generating these waveforms digitally contain aliasing due to having to round off the discontinuity time to the nearest available sampling instant. The signals primarily addressed here are the impulse train, rectangular pulse, and sawtooth waveforms. Because the latter two signals can be derived from the first by integration, only the algorithm for the impulse train is developed in detail.

My red words.

My pink ones.

#### Grumpy_Mike

#8
##### May 14, 2013, 10:37 pm
Maybe it is a language thing but you do not understand what you are saying.

#### Nantonos

#9
##### May 15, 2013, 11:13 am

Maybe it is a language thing but you do not understand what you are saying.

I believe I understand the words digital and synthesis fairly well. And bandlimited synthesis is a fairly well established concept.

However, if you are of the opinion that it is all a waste of time and totally unnecessary (and able to demonstrate this), perhaps you should publish your insight in a peer reviewed academic paper?

Go Up