Author Topic: Question: Cheap pure sine wave genertor 1khz?  (Read 18894 times)

0 Members and 1 Guest are viewing this topic.

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #25 on: January 11, 2020, 05:06:21 am »
I've had others recommend using the PC audio too, so I though I'ld give it try to see. I just wrote a quick hack to split 1kHz out of my Intel NUCs onboard audio, with the volume set to just below where bad harmonic distortion sets in (about 0.5V pk-pk) Spectrum is below.

If anybody has any good cheap sound dongle recommendations, I'm all ears...

Contents of Makefile:
Code: [Select]
all : sin
./sin | aplay -r 48000 -c 2 -f S16_BE

sin : sin.c
gcc -o sin sin.c -Wall -pedantic -O4 -lm
Contents of "sin.c":
Code: [Select]
#include <stdio.h>
#include <stdint.h>
#include <math.h>

int main(int argc, char *argv[]) {
    int i = 0;
    while(1) {
       double angle = i * 2 *M_PI / 48.0;
       double s = sin(angle);
       uint32_t u_sample = (s+1.0) * 16384;
       int32_t i_sample = u_sample - 16384;

       putchar((i_sample>>8) & 0xFF);
       putchar((i_sample>>0) & 0xFF);

       putchar((i_sample>>8) & 0xFF);
       putchar((i_sample>>0) & 0xFF);

       i = (i == 47 ? 0 : i+1) ;
    }
}
« Last Edit: January 11, 2020, 05:12:10 am by hamster_nz »
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 
The following users thanked this post: RoGeorge

Offline andy3055

  • Super Contributor
  • ***
  • Posts: 1111
  • Country: us
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #26 on: January 11, 2020, 05:06:36 am »
 

Offline innkeeperTopic starter

  • Supporter
  • ****
  • Posts: 615
  • Country: us
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #27 on: January 11, 2020, 06:22:02 am »
do you know the thd+n of the output signal?
Hobbyist and a retired engineer and possibly a test equipment addict, though, searching for the equipment to test for that.
 

Offline Marco

  • Super Contributor
  • ***
  • Posts: 6721
  • Country: nl
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #28 on: January 11, 2020, 07:44:25 am »
Vojtěch Janásek has a circuit described on his website, but maybe more importantly he also has a relatively simple circuit to knock down the fundamental frequency so you can measure the distortion with lower quality equipment.
 

Offline RoGeorge

  • Super Contributor
  • ***
  • Posts: 6202
  • Country: ro
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #29 on: January 11, 2020, 03:24:59 pm »
I've had others recommend using the PC audio too, so I though I'ld give it try to see. I just wrote a quick hack to split 1kHz out of my Intel NUCs onboard audio, with the volume set to just below where bad harmonic distortion sets in (about 0.5V pk-pk) Spectrum is below.

If anybody has any good cheap sound dongle recommendations, I'm all ears...

Contents of Makefile:
Code: [Select]
all : sin
./sin | aplay -r 48000 -c 2 -f S16_BE

sin : sin.c
gcc -o sin sin.c -Wall -pedantic -O4 -lm
Contents of "sin.c":
Code: [Select]
#include <stdio.h>
#include <stdint.h>
#include <math.h>

int main(int argc, char *argv[]) {
    int i = 0;
    while(1) {
       double angle = i * 2 *M_PI / 48.0;
       double s = sin(angle);
       uint32_t u_sample = (s+1.0) * 16384;
       int32_t i_sample = u_sample - 16384;

       putchar((i_sample>>8) & 0xFF);
       putchar((i_sample>>0) & 0xFF);

       putchar((i_sample>>8) & 0xFF);
       putchar((i_sample>>0) & 0xFF);

       i = (i == 47 ? 0 : i+1) ;
    }
}

Neat!   :-+
I didn't know there is a tool (aplay) that can take numbers from a pipe and throw them at the sound card.

Questions:
- is "i_sample>>0" there for readability only, and optimized out during compilation, or is it for something else?
- where from is the FFT screen capture, and why does it show a -14 dBV DC component?
- did you used some loopback between play and rec inside the audiocard, or physical cables?

Offline cncjerry

  • Supporter
  • ****
  • Posts: 1282
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #30 on: January 12, 2020, 08:43:34 pm »
I've found that you have to spend a fair amount on a sound card to get anything near the performance of Williams, Wien bridge, etc.  First off, the sound card has to be external and then you have USB noise to contend with.  You can fix some of that with good cables and opto isolated USB adapters and lifted grounds. But even then, and I've been messing with this stuff for 45yrs, it's tough to find something that will outperform those old lightbulb circuits.

One thing I didn't see was the DSA you are testing? I just bought an HP 3562a DSA and that is good for a boat anchor but the best it will do in range is 120db.  It is listed at 80db but there is a way I read someplace to get it to 120db.  It must be averaging or something or maybe that was the minimum signal.  I've only had it a few days.

One other thing, stop posting all the HP distortion analyzers or all I have to buy one soon.  I've wanted one and every time I get it out of my system, someone posts another picture.

Lastly, I was wondering is that I have HP Selective Level meters that are really accurate. I don't know how low they go but I was wondering if I can use one of them for THD testing.  I'll have to go back and see if they are flat down to 1khz.

Thanks,

Jerry
 

Offline cncjerry

  • Supporter
  • ****
  • Posts: 1282
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #31 on: January 12, 2020, 09:11:42 pm »
when you are generating sin waves you have to be aware of the large number problem. depending on how long you let that code run, sooner or later you are going to glitch. You need to generate the wave in an array so that the start at zero and the end will perfectly meet and then write the array blocks out.  I say blocks because I don't think, and I've not thought about it in a while, you can get a 'perfect' sine wave out of one block at 48k.  Here's a simple test. Calculate two 48k blocks generating the code as you did sequentially and then compare block 0 with block 48,000 and on to see if you have the same numbers.  Then write the file to disk and plot the spectrum before looping it out the card and back in. 

You should also plot your noise floor to see how much junk you have coming from the sound card.
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 16615
  • Country: us
  • DavidH
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #32 on: January 13, 2020, 02:15:09 am »
I've found that you have to spend a fair amount on a sound card to get anything near the performance of Williams, Wien bridge, etc.

No sound card will produce a signal as clean as the second example I gave which was designed for *testing* audio ADCs so necessarily has lower distortion.
 
The following users thanked this post: WattsThat

Online MasterT

  • Frequent Contributor
  • **
  • Posts: 785
  • Country: ca
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #33 on: January 13, 2020, 04:25:42 am »
I checked out AN67, sorry for my anglaise, but it's the most ridiculous /stupid circuits I've seen in a years.  180 dB, with funny two diodes? 
 

Offline Marco

  • Super Contributor
  • ***
  • Posts: 6721
  • Country: nl
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #34 on: January 13, 2020, 06:32:50 am »
It does look like an April fools joke doesn't it ... you can't just have a controlled current source drive current into the overcompensation input of an opamp and not explain what the hell that's supposed to be doing.
« Last Edit: January 13, 2020, 04:34:18 pm by Marco »
 

Offline innkeeperTopic starter

  • Supporter
  • ****
  • Posts: 615
  • Country: us
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #35 on: January 13, 2020, 07:07:40 am »

One thing I didn't see was the DSA you are testing? I just bought an HP 3562a DSA and that is good for a boat anchor but the best it will do in range is 120db.  It is listed at 80db but there is a way I read someplace to get it to 120db.  It must be averaging or something or maybe that was the minimum signal.  I've only had it a few days.


I am looking for my manuals with the application n notes.
i know it has 150 DB of measurement range.
but the operators manual does say 80db dynamic range. 
not sure how that plays out doing thd....if its limited to 80db
damn if that is the case that sucks.. but well might have to find another method when i get down to low thd readings,
i know there are some tricks, but..thats all in the application notes not in the operators manual.

 I hope to play with it a bit again this week
« Last Edit: January 13, 2020, 07:13:57 am by innkeeper »
Hobbyist and a retired engineer and possibly a test equipment addict, though, searching for the equipment to test for that.
 

Offline cncjerry

  • Supporter
  • ****
  • Posts: 1282
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #36 on: January 13, 2020, 05:28:58 pm »
I went back and looked at my DSA and pulled out the section, attached 2nd, that speaks to the dynamic range in an automatic function that I wasn't familiar with. I also attached the noise floor but have to figure out how that compares on the DSA.  I don't know what DSA you are using, but I know some DSAs have lower noise floor and if you want to look at the 3rd harmonic, for instance, you just focus the analyzer there. You can't see the entire spectrum but you can calculated the THD by summing the harmonics.

This guy has a well formatted formula for THD calcs:

http://www.onmyphd.com/?p=thd.total.harmonic.distortion

He also has the thd+noise calc there.

I'm surprised the HP analyzers go that low automatically calculating THD.  I really want one, just haven't been able to find one locally.

Jerry
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 3697
  • Country: gb
  • Doing electronics since the 1960s...
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #37 on: January 13, 2020, 06:12:33 pm »
I would use two EPROMs to get 16 bit words, drive them from a synchronous counter, and feed the 16 bits of data into a 16 bit audio DAC :)

I designed such a thing for a friend of mine's (!) final year univ. project in 1978. We used fusible link PROMs back then, only 8 bits of data so not smooth, but frequency changes are instant and there is no amplitude bounce.

If you use a good DAC, no missing codes, you can get THD below 0.005%. Probably less by filtering the output.

Won't be dirt cheap admittedly.

I believe there is a way of connecting up shift registers in a feedback arrangement which gives you a sin(x) output, but you still need a DAC.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline Fungus

  • Super Contributor
  • ***
  • Posts: 16649
  • Country: 00
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #38 on: January 13, 2020, 07:23:49 pm »
I would use two EPROMs to get 16 bit words, drive them from a synchronous counter, and feed the 16 bits of data into a 16 bit audio DAC :)

Did you build the counter yourself using D-type flip-flops?  :popcorn:

These days we have microcontrollers that are a lot cheaper than EPROMs and can do the counter, the feeding of the DAC, and everything.

 

Offline trobbins

  • Frequent Contributor
  • **
  • Posts: 763
  • Country: au
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #39 on: January 13, 2020, 09:04:30 pm »
There are good external USB soundcard/interfaces around - I have a 15 year old EMU 0404 USB - and although I haven't used one, it seems stock product like FocusRite 2i2 are now pretty cheap new.

REW software allows insertion of selected harmonics (2 to 9) in to the test signal from its tone generator, with settable magnitude and phase.  REW also provides bandwidth limiting and distortion component measurements of the loopback spectrum.

Using my EMU0404 USB as DUT with loopback, I did some testing last year and had 0.0006% 2nd and THD at 389.6Hz test tone when limiting bandwidth to just include 2nd harmonic. I can then null that out (0.0001%) with -104dB of added 2nd H with 90 deg phase shift.  I effectively did the same for higher order harmonics. 

This method will work for any typical soundcard, so the generation of a very low harmonic distortion waveform is not limited to the raw performance of the soundcard itself.  As such, a cheap or even zero-cost, and quick setup can be prepared by most.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #40 on: January 13, 2020, 09:27:28 pm »
I think this will be my next project - a < $10 low distortion 1kHz generator.

Thinking of using a Wein Bridge, but with an LED coupled to an LDR to replace the classic light build to control gain. Bit worried about that being non-linear though, and spoiling the result.
« Last Edit: January 13, 2020, 10:07:56 pm by hamster_nz »
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline Gyro

  • Super Contributor
  • ***
  • Posts: 9504
  • Country: gb
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #41 on: January 13, 2020, 09:51:44 pm »
The best ones do seem to use LED+LDR. There's effectively zero voltage modulation of the LDR resistance.
Best Regards, Chris
 

Online MasterT

  • Frequent Contributor
  • **
  • Posts: 785
  • Country: ca
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #42 on: January 13, 2020, 10:00:47 pm »
...
Using my EMU0404 USB as DUT with loopback, I did some testing last year and had 0.0006% 2nd and THD at 389.6Hz test tone when limiting bandwidth to just include 2nd harmonic. I can then null that out (0.0001%) with -104dB of added 2nd H with 90 deg phase shift.  I effectively did the same for higher order harmonics. 

This method will work for any typical soundcard, so the generation of a very low harmonic distortion waveform is not limited to the raw performance of the soundcard itself.  As such, a cheap or even zero-cost, and quick setup can be prepared by most.
This trick IS limited by performance of the adc. I don't know what kind of chipset inside EMU0404, but I doubt it has THD+Noise level below 0.0001% (-120dBc).  I'm working on similar project, doing software correction THD by magnitude and phase tracking in antiphase on stm32F767 dual dac DDS generator. The problem I have is non-linearity of AD7984, about -110 dBc, so can't break this barrier and my DDS always has THD above this flour.
 

Online MasterT

  • Frequent Contributor
  • **
  • Posts: 785
  • Country: ca
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #43 on: January 13, 2020, 10:11:48 pm »
The best ones do seem to use LED+LDR. There's effectively zero voltage modulation of the LDR resistance.
Page 35, distortion.
 
The following users thanked this post: Gyro

Offline trobbins

  • Frequent Contributor
  • **
  • Posts: 763
  • Country: au
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #44 on: January 13, 2020, 10:41:02 pm »
This trick IS limited by performance of the adc.
The technique also has to contend with the measurement null being the result of both DAC and ADC, and not just DAC, so some deduction needed to confirm that DAC result by itself is nulled.

Noise level will depend on a few factors, so not straightforward to report on.  The mid-band noise level for that EMU soundcard is down around -130dB from full-scale signal, and uses 24-bit 192 kHz AKM AK5385A ADC and AK4396 DAC.

Still, the trick can also be used to null out any additional harmonic distortion from added devices up to the point of DUT connection, so could be applied to an add-on signal amplifier if needed for the DUT and test environment.  The power of the technique is simplicity and ability to make a mid-range silk purse out of a sows ear.
 

Offline Vovk_Z

  • Super Contributor
  • ***
  • Posts: 1417
  • Country: ua
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #45 on: January 13, 2020, 10:47:34 pm »
i've hit up victor from the diyaudio forum to see if he has any of his boards available. he is getting better then -150 Db.
What circuits? I've seen link to e-b https://www.ebay.com/itm/Low-Distortion-Audio-Range-Oscillator-1KHz-Sine-Wave-Signal-Generator-PCB-Board/262844562138,
 let's call it JFET-Wein, description says:
Quote
Low distortion coefficient of the sinusoidal oscillator circuit board, oscillation frequency: 1KHz, low distortion, distortion factor 0.0064% (- 124dB)
- this one isn't Victor's, it's unnamed RC-oscillator. It really have 0.006%, but have a potential to go to 0.001% with smal change (resistor "120-150k" change to value 27-30k if I'm not wrong). Best side of it that it is variable frequency with stable amplitude.
Victor's oscillator is constant frequency and ultra low distortion.
 

Offline f5r5e5d

  • Frequent Contributor
  • **
  • Posts: 349
 

Online MasterT

  • Frequent Contributor
  • **
  • Posts: 785
  • Country: ca
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #47 on: January 14, 2020, 12:12:17 am »
This trick IS limited by performance of the adc.
...
Noise level will depend on a few factors, so not straightforward to report on.  The mid-band noise level for that EMU soundcard is down around -130dB from full-scale signal, and uses 24-bit 192 kHz AKM AK5385A ADC and AK4396 DAC.
Dreams, dreams... Based on a DS from this http://www.datasheetcatalog.com/datasheets_pdf/A/K/5/3/AK5385A.shtml
THD of the appointed ADC is -103 dBc. Period. Nothing could be measured below this level. More than that, depends on a driver analog front end , likely results never cross -100dB . I see here https://www.soundonsound.com/reviews/emu-0404 they honestly print 0.001% THD+N. No where close to -130dB.
 

Offline rhb

  • Super Contributor
  • ***
  • Posts: 3483
  • Country: us
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #48 on: January 14, 2020, 03:10:20 am »
Unless I were prepared to launch into a serious development project, I'd use a sound card.

Write .WAV files covering the test amplitudes and frequencies.

Play them back through the sound card.

Record L->L, L->R, R->L &  R->R

Solve for the input and output errors of each channel and correct.  At each frequency and amplitude there are 4 equations and 4 unknowns.

You can get almost arbitrary accuracy via signal processing with large datasets.

Otherwise, I'd recommend building a single frequency version of  Jim Williams last version from "Max Wien, Mr. Hewlett and a Rainy Sunday Afternoon" using well matched fixed parts.  But then you have to design and test the distortion analyzer.

Having lived through the THD wars in audio gear in the late 70's I feel obliged to point out that the distortion of the best sound transducers (microphone and speaker) are an order of magnitude larger than a cheap audio IC.

Have Fun!
Reg
 

Online MasterT

  • Frequent Contributor
  • **
  • Posts: 785
  • Country: ca
Re: Question: Cheap pure sine wave genertor 1khz?
« Reply #49 on: January 14, 2020, 03:32:06 am »
...
Record L->L, L->R, R->L &  R->R

Solve for the input and output errors of each channel and correct.  At each frequency and amplitude there are 4 equations and 4 unknowns.

You can get almost arbitrary accuracy via signal processing with large datasets.

No, you can't. The issue is that harmonic's  magnitude & phase (thd-n) are not  constants. Doesn't matter how big  data sets  are, how accurate equation written or what precision of the floating math. The data becomes a junk after a few milliseconds, because ADC is drifted, always changing - there is no single static constant magnitude & phase below -100 dBc with audio ADC, or -110 with precise SAR.   Same with a DAC. During debugging my project, I came to conclusion that uCPU has to continuously monitor each thd-n and make adjustment "on the fly" .
Calibration & look-up table doesn't work, actually it does for stm32f767 internal 12-bits DAC with initial start-up thd-3 about -70 dBc, LUT could fix this value to -95/-100, but than magic show begins.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf