Digital Audio Sinewave Generator, a project draft

Go To Last Post
37 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lately, I needed to build an audio sinewave generator to produce, as a first step, 30 frequencies around the ones listed below (18 KHz to 615 Hz):

 

18,000
15,500
13,500
12,000
10,800
 9,800
 9,000
 7,700
 6,760
 6,000
 5,400
 4,900
 4,500
 3,850
 3,380
 3,000
 2,700
 2,450
 2,250
 1,930
 1,690
 1,500
 1,350
 1,230
 1,130
   965
   845
   750
   675
   615

The exact frequency values are not important as long they are close (tolerance +/- 0.5%) to the ones listed above.

 

I have to use ATmega8L only with an external 8 MHz crystal, and some general purpose resistors and capacitors.

But, with these limited components, I also have to satisfy the following conditions:

 

[1] The 30 signals have just one passive RC filter.

[2] Their output amplitudes could be at any level as long they are close to each other (tolerance +/- 5%).

[3] Their distortions (THD) have to be less than 1% (audio) and less than 2% (total).

 

My question is:

Do you think that the saying “necessity is the mother of invention” could be applied in this case too?

 

Thank you.

Kerim

 

Last Edited: Thu. Feb 13, 2020 - 12:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I’d use dds and a switched capacitor filter.
If there were no constraints, then why would you need an engineer!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

And if I live elsewhere I would use one of the digital sinewave generator ICs smiley

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Which probably use dds and switched capacitor techniques.......

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think so, too

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That’s not to say you couldn’t use a wien bridge oscillator then freq and amplitude lock by switching capacitors and/or resistors.

Or a permeability tuned oscillator or .......

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess you mean certain analogue solutions by using active devices other than ATmega8L.

I look for a digital generator so that, when the project is done, the frequency values are fixed and known.

And once the MCU (here ATmega8L) gets the index number of the frequency (from 0 to 29 as a first step), it generates the requested signal. This request could be done by manual switches (5 now) or via TWI connection from another MCU.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KerimF wrote:

...as a first step...

 

So will the specification change in future if this is just the first step?

 

KerimF wrote:

I have to use ATmega8L only...

 

So no other chips allowed?

 

KerimF wrote:

[1] The 30 signals have just one passive RC filter.

 

So just one R and one C?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I meant by "as a first step" just the number of frequencies. 30 is actually the minimum number in this upper audio bandwidth. (Added: I mean, it could be increased in the future).

Yes, the active device of the generator is ATmega8L only... which I can get smiley

 

About the passive RC filter, any number of Rs and Cs could be used.

 

Last Edited: Thu. Feb 13, 2020 - 02:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Its been too many years ago since I did all of the FFT calculations to "prove" it, but from experience I doubt you will be able to meet those spec's.

 

The issue is that a DDS is likely the "best" way to generate the broad range of frequencies you need.

It will generate an output sine wave with a fixed, constant amplitude.

 

To meet your distortion requirements you need a pretty good LPF on the DDS output to remove the higher frequency harmonics embedded within the signal.

The DDS output is a series of small stair-steps, and those sharp cornered stair steps contain lots of high frequency harmonics and energy.

The filter will easily remove those and give you a very clean sine wave.

 

The problem is that the "passive RC" filter's transfer function is a function of frequency.

It isn't a perfect LPF.

As the input signal goes up in frequency, the filter will cause greater attenuation of the desired signal as well as attenuating the harmonics.

So the amplitude won't be constant across the full frequency range.

 

The next concern is that the output likely has to drive something, or else why build the device at all.

The device you connect the RC filter too can "load" the filter, and change its characteristics.

That is why one would typically have an op-amp to provide the final output signal, and isolate the load from the signal generator.

 

So, if you have an op-amp available then why not use it for the LPF?

(You stated the spec is only a passive RC filter).

 

If you had a twin op-amp package it would be easy to have a 4th order LPF and have the op-amp (filter) drive the output.

It will still be a challenging spec as the harmonics for the 600 Hz signal are in the region of the 1800 Hz signal.

 

That is what a switched cap filter was mentioned above, but that is a far from passive device!

 

If you go the DDS route, then the higher the micro's clock the "better" the original sine wave that is produced by the micro.

So using 16 MHz would be a better option, if you can include an Xtal in the design.

If you want any form of accuracy for the frequency you likely want to use an Xtal and not the internal RC Osc anyway.

 

You also need to think about how you will actually generate the output signal.

The audio guys have a nice rule-of-thumb for how many bits in an R-2R resistor ladder one needs for a given distortion.

Using an R-2R ladder for the output requires very well matched resistors to meet your distortion requirements.

(Both the number of steps, and how well the steps are matched, impact the output signal).

 

If you don't want to use the R-2R for the output then you can use a PWM output to generate the output signal. 

 

Of course if you had an Xmega available you would have a 32 MHz (in spec, or low 40's MHz overclocked) micro with an internal DAC module.

So the signal generation becomes much easier.

 

Your filter requirements, however, still make this a challenging project.

 

JC

 

Edit: Typo

Last Edited: Thu. Feb 13, 2020 - 06:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

This is one of my favorite photos, I've posted it a time or two before, but it is relevant to this discussion as well.

 

The top signal is a DDS "sine wave", generated with very few steps.

It is obviously a pretty crude sine wave at best.

 

Feed it through a two stage op-amp LPF and you get the bottom signal.

The harmonics are gone, and one gets just the fundamental frequency, and a nice looking sine wave.

 

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi JC,

 

First, I like to thank you for the interesting analysis of the subject.

 

Second, you are totally right. Using DDS, R-2R ladder or PWM cannot help in this case. And, as you said, the output of the passive RC filter should be buffered. Actually, it will be followed by one (or two opamps) in order to control the end amplitude from 0V to Vmax (but this is another story smiley ).

 

So I had no choice but to think out of the box to fulfil the three conditions above.

 

After days of work (since here 6/24 of electricity only and no enough fuel to warm the room in which I work), I was able generating all 30 frequencies with rather very low THD  (condition 3). The signals at the output of the simple RC filter (condition 1) are like the sinewave on your scope (#11). I am trying now to decrease, as possible, the difference among the generated sinewave amplitudes since it is now around +/- 20% (not +/- 5%).

 

I have no doubt that if you found yourself facing the same conditions, you would surely think of the solution the way I did.

 

Kerim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would try using one of the PWM outputs on timer1, feed it a sine wave table value and filter with a simple RC filter, since the spec in the OP says output amplitude is not important,

you may get away with a single RC low pass filter for most of the range, experiment with values of R and C.

I would start with 10k and 100nf, see what you come up with to meet the low end distortion value and how low the signal will be at the high end.

Adjust R and C as needed, this is probably what your teacher wants you to learn.

I can provide example code to get you started.

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Thu. Feb 13, 2020 - 08:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My teacher?! PWM?! Aren't you kidding, Jim laugh

But perhaps this is the first time you heard of me cheeky

 

If you are serious, you will real surprise me if you do think that, by using PWM (of ATmega8L), one can generate very low THD sinewaves for 18 kHz and 615 Hz (the two limits on the OP list) by using just one passive RC low-pass filter (of any degree).

 

By the way, this project is about the main part of one of my future products.

 

Kerim

 

Added: Please note the condition 2, on OP, concerning the output amplitudes.

 

 

 

Last Edited: Thu. Feb 13, 2020 - 09:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KerimF wrote:

Lately, I needed to build an audio sinewave generator to produce, as a first step, 30 frequencies around the ones listed below (18 KHz to 615 Hz):

 

[1] The 30 signals have just one passive RC filter.

[2] Their output amplitudes could be at any level as long they are close to each other (tolerance +/- 5%).

[3] Their distortions (THD) have to be less than 1% (audio) and less than 2% (total).

...

 I am trying now to decrease, as possible, the difference among the generated sinewave amplitudes since it is now around +/- 20% (not +/- 5%).

 

Sounds like you are getting close.

If your LPF is skewing the amplitude, one solution could be to use a 8/12b R-2R DAC, where the highest frequencies use 8b for speed, and at some middle value,  you switch to 12b, which greatly improves the distortion of the lowest Hz 

That then relaxes the LPF somewhat.

Or, you may find expanding the table is enough, with 256 steps in Y axis, you can usefully have 1024 LUT entries and those will all be used below ~ 868 Hz 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your suggestions are good, Wh0-me.

But the "necessity" here is that, besides the passive RC filter, there are ATmega8L and 8 MHz crystal only... nothing else smiley

 

By the way, I tend to believe that the idea (I got while dreaming in bed smiley ) of the method\routine which made possible to build this digital audio sinewave generator with the OP constraints cannot be found on any website. The reason is simple; its seems no one in the world, but I, needed it in the first place angel

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KerimF wrote:

But the "necessity" here is that, besides the passive RC filter, there are ATmega8L and 8 MHz crystal only... nothing else smiley

 

Your OP was this 

KerimF wrote:

I have to use ATmega8L only with an external 8 MHz crystal, and some general purpose resistors and capacitors.

 

So the R-2R is certainly made from  'some general purpose resistors' - the point is the finer the steps, the easier the filter's task.

You may also be able to use port pins as CMOS switches, to change the LPF caps. 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, your two points are valid indeed.

 

But it happens that my routine/code lets just one passive RC filter do its job properly; by giving a low THD sinewave output for all frequencies on the list.

And this fact should be considered as an interesting hint to guess the routine/code which I had the chance to think of.

  

 

Last Edited: Thu. Feb 13, 2020 - 11:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just me being curious.....

 

Is this a one off project for yourself or mass market?

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kerim, if you like interesting ways to generate sine waves, have you read any of Don Lancaster's work?

 

Search terms are "magic sinewaves" "step-locked sinewaves" "walking ring sinewaves".

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi JIm,

 

As I mentioned earlier, I needed to build this rather simple generator (which will be made to cover the audio bandwidth, from 20 Hz to 20 KHz) for one of my future products.

But implementing it properly (to satisfy certain conditions) takes many days of work, in my case, since the only tools, I have, for my calculations are excel and LTspice.

 

Kerim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian, so you know already how an ATmega8L and a simple passive RC filter can generate sinewaves from 615b Hz up to 18 KHz with low THD (at any frequency) cheeky

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I will upload my work, when finished, as a project in memory of ATMEL; the last American company that didn’t have to close its door every time I knocked on it, for more than 25 years.

 

From this project, you will also deduce that its technique can be used to generate hundreds (if not thousands) sinewaves of frequencies between 615 Hz and 18 KHz with the same RC filter (same R & C values).

 

In the same way, it is possible to generate sinewaves in the lower audio spectrum (20 Hz to 600 Hz) but by using an MCU pin to switch on a few capacitors into the previous RC filter (via NPN transistors for example) for these lower frequencies.

 

And I believe that many engineers around the world will exploit its technique to also generate ultrasonic and subsonic frequencies by making minor changes and/or additions.

 

So till the time when I will upload this project, I think the “necessity” to many friends here is, now, to surprise me... by their discovery of what I did.

 

Have fun.

 

Cheers,

Kerim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Keri,
Check your PM.

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry Jim, it seems you forgot that I can access the forums only sad

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KerimF wrote:

Sorry Jim, it seems you forgot that I can access the forums only sad

 

I don't understand, you cannot read Private Messages?

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I can't read any PM... and even I can't update my profile. In other words, when I try accessing other than the AVR forums I just get a blank page... till I feel bored and close it smiley

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kerim,

 

Would this work if you had them:

 

https://www.sparkfun.com/datashe...

 

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you, Jim, for reminding me the analogue IC (XR2206) by which I built, about 40 years ago, the first two signal generators for my various tests (both are still working smiley ).

But now, using a digital generator is crucial for my future product. Frequencies should be fixed and known.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KerimF wrote:
But now, using a digital generator is crucial for my future product.

 

IC, (Pun intended)

 

I was going to send you some if you wanted them, but I guess theres no need.

 

CHEERIO!

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you, Jim, for your personal care.

 

By the way, if I was able to get anything from abroad (paid or gifts) in these days, I wouldn't have the necessity to dream of a new technique to build a simple low-cost digital sinewave generator with what the local retailers can offer me as electronic components. smiley

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KerimF wrote:

...what the local retailers can offer me as electronic components.

 

I am genuinely interested in what sort of components you are able to get, both from local stores (do you have those?) and via the mail. And what sort of prices you have to pay.

 

Here in the UK we have lost nearly all our local electronics stores. I think I would struggle to find more than 6 in the whole of the UK who could sell me a 2k7 1% resistor over the counter.

 

On the other hand we are spoilt for choice with online sellers. I regularly deal with 4 or 5 UK based companies who will have components on my desk by 11am even if I order at 7pm the previous night. And if I'm in a real hurry I am less than an hours drive from RS Components' main office.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
Here in the UK we have lost nearly all our local electronics stores

Not that there were ever many!

 

. I think I would struggle to find more than 6 in the whole of the UK who could sell me a 2k7 1% resistor over the counter.

indeed.

 

if I'm in a real hurry I am less than an hours drive from RS Components' main office.

Is that the round trip, or each way?

 

For me, it's about an hour each way to the nearest RS trade counter.

factoring-in my time and the mileage cost, it'd probably be cheaper to pay for their express courier delivery.

unless I happen to be going that way anyhow ...

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Brian Fairchild wrote:

Here in the UK we have lost nearly all our local electronics stores.

Not that there were ever many!

 

In Cambridge we had the Aladdin's cave that was H Gees. But in general we do seem to have done poorly in comparison to some places.

 

awneil wrote:

Is that the round trip, or each way?

 

44.1 miles is my shortest route according to Google.

 

 

I've been there twice when it was the only option to get something out of the door.

 

But generally I just use online suppliers. An order from Rapid is usually delivered around 9:30, Farnell around 11:00, CPC around 11:00 if I pay the £2.99 upgrade, and RS towards 16:00.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian wrote:

I am genuinely interested in what sort of components you are able to get, both from local stores (do you have those?) and via the mail. And what sort of prices you have to pay.


 

Life here after March 2011 is almost the inverse of life we have had before that date (likely by a supernatural decision smiley , our paradise was transformed to hell).

 

Before 2011, it was enough for me to design and draw the necessary schematics (using Kicad) for a new product and send them to a Chinese company. There, the Chinese engineers draw for me the PCB layouts so that they could produce and test the assembled boards before sending them to me. Obviously, they got all components from China while we agreed to use genuine ones only by paying them a higher price.

 

After 2011, the ordinary people (as I) who are living here have been isolated, step by step and year after year, economically by decisions made by certain world's Elite (besides terrorism). So many local people (in millions) had to flee while I stayed home. I mean; one just needs to imagine how people lived their World Wars 1 and 2, to get how today's people live their World War 3 (which was planned to last many decades to come while it hits one region after another, every 10 years indecision).

 

By the way, I pay now for ATmega8L (100 pcs) what is equivalent to 1$ for each, and 3$ for 1000 resistors 1/2W 5%.

The luxury of buying and selling via mail (via local transport) is now a thing of the past... till new decisions will be made to allow us return to normal life smiley

 

  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is Crickelwood Electronics still operating? I used them when I lived closer to the UK.

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

valusoft wrote:

Is Crickelwood Electronics still operating? I used them when I lived closer to the UK.

 

Yes.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss