Pages: [1]   Go Down
Author Topic: Bandlimited waveform generation  (Read 1205 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27114
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34220
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Brilliant! If I don't have to worry about it then i'm happy not doing so!

Thanks,
Harris
Logged

Nice, France
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34220
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Nice, France
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34220
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Nice, France
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Pages: [1]   Go Up
Jump to: