Author Topic: [Solved] Best way to amplify a sine wave without distorsion.  (Read 32456 times)

0 Members and 1 Guest are viewing this topic.

Offline LvW

  • Frequent Contributor
  • **
  • Posts: 282
  • Country: de
Re: Best way to amplify a sine wave without distorsion.
« Reply #25 on: January 27, 2017, 02:59:55 pm »
Google returned many interesting options when I searched for: 3rd order notch filter
Of course you need the "opposite" of a notch filter, but it is trivial to invert a filter circuit to make a sharp (high-Q) bandpass filter.

Did you ever try to solve such a "trivial" task? I doubt. There are specific bandpass design procedures which can (and should) be applied.
 

Offline Richard Crowley

  • Super Contributor
  • ***
  • Posts: 4317
  • Country: us
  • KJ7YLK
Re: Best way to amplify a sine wave without distorsion.
« Reply #26 on: January 27, 2017, 04:42:49 pm »
Did you ever try to solve such a "trivial" task? I doubt. There are specific bandpass design procedures which can (and should) be applied.
Yes I have. Sorry you found it so difficult.
 

Offline HoracioDosTopic starter

  • Frequent Contributor
  • **
  • Posts: 344
  • Country: ar
  • Just an IT monkey with a DSO
Re: Best way to amplify a sine wave without distorsion.
« Reply #27 on: January 27, 2017, 07:08:08 pm »
a) I first began using a LM741 to generate a sinewave and then switched to a LM358.

I think I found the crux of the matter in my own answer.

a) I replaced LM741 with LM358 in the last minute. As the pinouts are quite similar I made corrections in the breadboard but forgot to do them in the schematics.  :palm:
b) Q1 and Q2 were OK. Q1 Collector was connected to Q2 Gate and Q2 Source to R10. But... R10 was overlaping Q1 so it seemed that R10 was connected to Q1 Collector and to Q2 Gate.
c) Also R11 and R12 were missing.  :palm:

I updated the schematic. It should be fine now. (Finger crossed)
So I'm back on track to find out a proper way how to amplify the sine wave.
« Last Edit: January 27, 2017, 07:09:52 pm by HoracioDos »
 

Offline Audioguru

  • Super Contributor
  • ***
  • Posts: 1507
  • Country: ca
Re: Best way to amplify a sine wave without distorsion.
« Reply #28 on: January 27, 2017, 07:29:38 pm »
1) Your opamp used in the Wien Bridge oscillator will produce a clipped sinewave because it is missing an automatic gain control (a light bulb or a Jfet).
2) The output of the LM386 power amp will have its positive excursion clipped because it amplifies the DC from the Mosfet. A coupling capacitor to block the DC is needed between the R10 volume control and the pin 3 input of the LM386.
 
 
The following users thanked this post: HoracioDos

Offline HoracioDosTopic starter

  • Frequent Contributor
  • **
  • Posts: 344
  • Country: ar
  • Just an IT monkey with a DSO
Re: Best way to amplify a sine wave without distorsion.
« Reply #29 on: January 27, 2017, 09:13:32 pm »
Requiescat in pace Fritzing. There is no light bulb or jfet n-channel parts. Moving to Kicad this weekend. It's going to take a while...

1) Your opamp used in the Wien Bridge oscillator will produce a clipped sinewave because it is missing an automatic gain control (a light bulb or a Jfet).

It seems that the easiest change could be replacing R4 with a 5V light bulb (light bulbs are chasing me after all) and R3 with a 100K trimpot. It will allow me to find the correct value later. If I use a JFet N-Channel. Can a 2N5457/8 or BF244A/5A be used?

2) The output of the LM386 power amp will have its positive excursion clipped because it amplifies the DC from the Mosfet. A coupling capacitor to block the DC is needed between the R10 volume control and the pin 3 input of the LM386.

I have many 0.1uF (104) capacitors. I guess It could work.
 

Offline Audioguru

  • Super Contributor
  • ***
  • Posts: 1507
  • Country: ca
Re: Best way to amplify a sine wave without distorsion.
« Reply #30 on: January 28, 2017, 12:14:36 am »
It seems that the easiest change could be replacing R4 with a 5V light bulb (light bulbs are chasing me after all) and R3 with a 100K trimpot.
You will not find a light bulb with a warm resistance as high as 47k ohms so the value of R3 must be reduced a lot. You probably know that the opamp gain for a Wien bridge oscillator must be about 3. Also you need to use a power amplifier to drive the light bulb instead of an opamp.

Quote
If I use a JFet N-Channel. Can a 2N5457/8 or BF244A/5A be used?
A 2N5459 or a 2N5486 has a lower range of cutoff voltage. You should also re-design the attenuator so that it has no DC voltage on it (your R6 causes a huge POP and should be removed).

Quote
I have many 0.1uF (104) capacitors. I guess It could work.
Don't guess, look at the input resistance of 50k ohms for the LM386 in its datasheet then calculate that a 0.1uF capacitor will pass frequencies above about 32Hz.
« Last Edit: January 28, 2017, 12:19:09 am by Audioguru »
 
The following users thanked this post: HoracioDos

Online Zero999

  • Super Contributor
  • ***
  • Posts: 19514
  • Country: gb
  • 0999
Re: Best way to amplify a sine wave without distorsion.
« Reply #31 on: January 28, 2017, 12:30:58 am »
The LM386 can be used as a Wein bridge oscillator with a J-FET AGC circuit, rather than the light bulb suggested on the data sheet.

 
The following users thanked this post: HoracioDos

Offline Audioguru

  • Super Contributor
  • ***
  • Posts: 1507
  • Country: ca
Re: Best way to amplify a sine wave without distorsion.
« Reply #32 on: January 28, 2017, 01:42:45 am »
The datasheet for the LM386 says that it becomes unstable if its gain is reduced to less than 9  but here it is 3. Do you think the capacitor between pin 1 and pin 8 reduces its high frequencies enough to make it stable with a gain of only 3?
 
The following users thanked this post: HoracioDos

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2365
  • Country: de
    • Frank Buss
Re: Best way to amplify a sine wave without distorsion.
« Reply #33 on: January 28, 2017, 06:58:43 am »
Another easy way to create a clean and stable sin wave is to use a DAC. I tried it with a MAX5712, a 12 bit DAC, which can be directly connected to the Arduino with SPI. This is a test with a Mega2560, which allows up to 24 kHz samplerate, here with 20 kHz samplerate, generating a sin wave of 943 Hz:



Only one small 6 pin IC, two capacitors and one resistor are needed, and you can change the frequency in software, and switch it on and off without more external parts. You can even program it in a way, that it switches it off at zero crossover and starts at zero (1/2 VDD with the current circuit), to avoid any clicks, or add an envelope. If you need more power for the output, you can still use your LM386. Depending on the output frequency and the samplerate, you need to adjust the lowpass filter with R1/C1. Unlike most audio DACs, the output of the MAX5712 can be DC. This is the filtered output for 0.1 Hz, only one variable change in the Arduino sketch:



Below is the Arduino source code. The output is generated in an interrupt, you can use the loop for other things like reading switches or analog values, blinking LEDs etc. But the interrupt uses timer 1, which means you can't use the Arduino PWM functions anymore. For low frequencies, you can even use the sinf function for calculating the DAC value in realtime, no need for the big table, but this works only up to 5 kHz samplerate. I tried this with a STM32F4 discovery kit, which has an ARM microcontroller, and it is no problem to calculate the sinf function @48 kHz samplerate in stereo and 24 bit in realtime, if running with 168 MHz CPU clock.

Code: [Select]
/**
 * Generates a sin 943 Hz wave with a MAX5712, connected with SPI. Tested with a Mega2560 Arduino.
 * Samplerate works up to 24 kHz.
 * SCK: pin 52
 * MOSI: pin 51
 */
#include "SPI.h"

// pin numbers
const int ledPin = 13;
const int csPin = 12;

// signal generator
const float samplerate = 20000.0f;
const float frequency = 943.0f;
uint32_t increment = 1024.0f / samplerate * frequency * 4194304;  // multiplicator for 22 bit shift
uint32_t sinIndex = 0;
uint16_t nextValue = 0;

// 1024 values sin table, 12 bit unsigned
const uint16_t sinTable[] = {
  0x0800, 0x080c, 0x0819, 0x0825, 0x0832, 0x083e, 0x084b, 0x0857, 0x0864, 0x0870, 0x087d, 0x088a, 0x0896, 0x08a3, 0x08af, 0x08bc,
  0x08c8, 0x08d5, 0x08e1, 0x08ee, 0x08fa, 0x0907, 0x0913, 0x091f, 0x092c, 0x0938, 0x0945, 0x0951, 0x095d, 0x096a, 0x0976, 0x0983,
  0x098f, 0x099b, 0x09a7, 0x09b4, 0x09c0, 0x09cc, 0x09d8, 0x09e5, 0x09f1, 0x09fd, 0x0a09, 0x0a15, 0x0a21, 0x0a2e, 0x0a3a, 0x0a46,
  0x0a52, 0x0a5e, 0x0a6a, 0x0a76, 0x0a82, 0x0a8e, 0x0a99, 0x0aa5, 0x0ab1, 0x0abd, 0x0ac9, 0x0ad4, 0x0ae0, 0x0aec, 0x0af8, 0x0b03,
  0x0b0f, 0x0b1a, 0x0b26, 0x0b32, 0x0b3d, 0x0b48, 0x0b54, 0x0b5f, 0x0b6b, 0x0b76, 0x0b81, 0x0b8d, 0x0b98, 0x0ba3, 0x0bae, 0x0bb9,
  0x0bc4, 0x0bd0, 0x0bdb, 0x0be6, 0x0bf0, 0x0bfb, 0x0c06, 0x0c11, 0x0c1c, 0x0c27, 0x0c31, 0x0c3c, 0x0c47, 0x0c51, 0x0c5c, 0x0c66,
  0x0c71, 0x0c7b, 0x0c86, 0x0c90, 0x0c9a, 0x0ca4, 0x0caf, 0x0cb9, 0x0cc3,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0x0e0e, 0x0e05, 0x0dfd, 0x0df5, 0x0dec, 0x0de4, 0x0ddb, 0x0dd3, 0x0dca, 0x0dc1, 0x0db9, 0x0db0,
  0x0da7, 0x0d9e, 0x0d95, 0x0d8c, 0x0d83, 0x0d7a, 0x0d71, 0x0d67, 0x0d5e, 0x0d55, 0x0d4b, 0x0d42, 0x0d39, 0x0d2f, 0x0d25, 0x0d1c,
  0x0d12, 0x0d08, 0x0cff, 0x0cf5, 0x0ceb, 0x0ce1, 0x0cd7, 0x0ccd, 0x0cc3, 0x0cb9, 0x0caf, 0x0ca4, 0x0c9a, 0x0c90, 0x0c86, 0x0c7b,
  0x0c71, 0x0c66, 0x0c5c, 0x0c51, 0x0c47, 0x0c3c, 0x0c31, 0x0c27, 0x0c1c, 0x0c11, 0x0c06, 0x0bfb, 0x0bf0, 0x0be6, 0x0bdb, 0x0bd0,
  0x0bc4, 0x0bb9, 0x0bae, 0x0ba3, 0x0b98, 0x0b8d, 0x0b81, 0x0b76, 0x0b6b, 0x0b5f, 0x0b54, 0x0b48, 0x0b3d, 0x0b32, 0x0b26, 0x0b1a,
  0x0b0f, 0x0b03, 0x0af8, 0x0aec, 0x0ae0, 0x0ad4, 0x0ac9, 0x0abd, 0x0ab1, 0x0aa5, 0x0a99, 0x0a8e, 0x0a82, 0x0a76, 0x0a6a, 0x0a5e,
  0x0a52, 0x0a46, 0x0a3a, 0x0a2e, 0x0a21, 0x0a15, 0x0a09, 0x09fd, 0x09f1, 0x09e5, 0x09d8, 0x09cc, 0x09c0, 0x09b4, 0x09a7, 0x099b,
  0x098f, 0x0983, 0x0976, 0x096a, 0x095d, 0x0951, 0x0945, 0x0938, 0x092c, 0x091f, 0x0913, 0x0907, 0x08fa, 0x08ee, 0x08e1, 0x08d5,
  0x08c8, 0x08bc, 0x08af, 0x08a3, 0x0896, 0x088a, 0x087d, 0x0870, 0x0864, 0x0857, 0x084b, 0x083e, 0x0832, 0x0825, 0x0819, 0x080c,
  0x0800, 0x07f3, 0x07e6, 0x07da, 0x07cd, 0x07c1, 0x07b4, 0x07a8, 0x079b, 0x078f, 0x0782, 0x0775, 0x0769, 0x075c, 0x0750, 0x0743,
  0x0737, 0x072a, 0x071e, 0x0711, 0x0705, 0x06f8, 0x06ec, 0x06e0, 0x06d3, 0x06c7, 0x06ba, 0x06ae, 0x06a2, 0x0695, 0x0689, 0x067c,
  0x0670, 0x0664, 0x0658, 0x064b, 0x063f, 0x0633, 0x0627, 0x061a, 0x060e, 0x0602, 0x05f6, 0x05ea, 0x05de, 0x05d1, 0x05c5, 0x05b9,
  0x05ad, 0x05a1, 0x0595, 0x0589, 0x057d, 0x0571, 0x0566, 0x055a, 0x054e, 0x0542, 0x0536, 0x052b, 0x051f, 0x0513, 0x0507, 0x04fc,
  0x04f0, 0x04e5, 0x04d9, 0x04cd, 0x04c2, 0x04b7, 0x04ab, 0x04a0, 0x0494, 0x0489, 0x047e, 0x0472, 0x0467, 0x045c, 0x0451, 0x0446,
  0x043b, 0x042f, 0x0424, 0x0419, 0x040f, 0x0404, 0x03f9, 0x03ee, 0x03e3, 0x03d8, 0x03ce, 0x03c3, 0x03b8, 0x03ae, 0x03a3, 0x0399,
  0x038e, 0x0384, 0x0379, 0x036f, 0x0365, 0x035b, 0x0350, 0x0346, 0x033c, 0x0332, 0x0328, 0x031e, 0x0314, 0x030a, 0x0300, 0x02f7,
  0x02ed, 0x02e3, 0x02da, 0x02d0, 0x02c6, 0x02bd, 0x02b4, 0x02aa, 0x02a1, 0x0298, 0x028e, 0x0285, 0x027c, 0x0273, 0x026a, 0x0261,
  0x0258, 0x024f, 0x0246, 0x023e, 0x0235, 0x022c, 0x0224, 0x021b, 0x0213, 0x020a, 0x0202, 0x01fa, 0x01f1, 0x01e9, 0x01e1, 0x01d9,
  0x01d1, 0x01c9, 0x01c1, 0x01ba, 0x01b2, 0x01aa, 0x01a2, 0x019b, 0x0193, 0x018c, 0x0184, 0x017d, 0x0176, 0x016f, 0x0168, 0x0160,
  0x0159, 0x0153, 0x014c, 0x0145, 0x013e, 0x0137, 0x0131, 0x012a, 0x0124, 0x011d, 0x0117, 0x0111, 0x010a, 0x0104, 0x00fe, 0x00f8,
  0x00f2, 0x00ec, 0x00e7, 0x00e1, 0x00db, 0x00d5, 0x00d0, 0x00ca, 0x00c5, 0x00c0, 0x00ba, 0x00b5, 0x00b0, 0x00ab, 0x00a6, 0x00a1,
  0x009c, 0x0098, 0x0093, 0x008e, 0x008a, 0x0085, 0x0081, 0x007c, 0x0078, 0x0074, 0x0070, 0x006c, 0x0068, 0x0064, 0x0060, 0x005c,
  0x0059, 0x0055, 0x0051, 0x004e, 0x004b, 0x0047, 0x0044, 0x0041, 0x003e, 0x003b, 0x0038, 0x0035, 0x0032, 0x0030, 0x002d, 0x002a,
  0x0028, 0x0025, 0x0023, 0x0021, 0x001f, 0x001d, 0x001a, 0x0019, 0x0017, 0x0015, 0x0013, 0x0011, 0x0010, 0x000e, 0x000d, 0x000c,
  0x000a, 0x0009, 0x0008, 0x0007, 0x0006, 0x0005, 0x0004, 0x0004, 0x0003, 0x0002, 0x0002, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
  0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0002, 0x0002, 0x0003, 0x0004, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009,
  0x000a, 0x000c, 0x000d, 0x000e, 0x0010, 0x0011, 0x0013, 0x0015, 0x0017, 0x0019, 0x001a, 0x001d, 0x001f, 0x0021, 0x0023, 0x0025,
  0x0028, 0x002a, 0x002d, 0x0030, 0x0032, 0x0035, 0x0038, 0x003b, 0x003e, 0x0041, 0x0044, 0x0047, 0x004b, 0x004e, 0x0051, 0x0055,
  0x0059, 0x005c, 0x0060, 0x0064, 0x0068, 0x006c, 0x0070, 0x0074, 0x0078, 0x007c, 0x0081, 0x0085, 0x008a, 0x008e, 0x0093, 0x0098,
  0x009c, 0x00a1, 0x00a6, 0x00ab, 0x00b0, 0x00b5, 0x00ba, 0x00c0, 0x00c5, 0x00ca, 0x00d0, 0x00d5, 0x00db, 0x00e1, 0x00e7, 0x00ec,
  0x00f2, 0x00f8, 0x00fe, 0x0104, 0x010a, 0x0111, 0x0117, 0x011d, 0x0124, 0x012a, 0x0131, 0x0137, 0x013e, 0x0145, 0x014c, 0x0153,
  0x0159, 0x0160, 0x0168, 0x016f, 0x0176, 0x017d, 0x0184, 0x018c, 0x0193, 0x019b, 0x01a2, 0x01aa, 0x01b2, 0x01ba, 0x01c1, 0x01c9,
  0x01d1, 0x01d9, 0x01e1, 0x01e9, 0x01f1, 0x01fa, 0x0202, 0x020a, 0x0213, 0x021b, 0x0224, 0x022c, 0x0235, 0x023e, 0x0246, 0x024f,
  0x0258, 0x0261, 0x026a, 0x0273, 0x027c, 0x0285, 0x028e, 0x0298, 0x02a1, 0x02aa, 0x02b4, 0x02bd, 0x02c6, 0x02d0, 0x02da, 0x02e3,
  0x02ed, 0x02f7, 0x0300, 0x030a, 0x0314, 0x031e, 0x0328, 0x0332, 0x033c, 0x0346, 0x0350, 0x035b, 0x0365, 0x036f, 0x0379, 0x0384,
  0x038e, 0x0399, 0x03a3, 0x03ae, 0x03b8, 0x03c3, 0x03ce, 0x03d8, 0x03e3, 0x03ee, 0x03f9, 0x0404, 0x040f, 0x0419, 0x0424, 0x042f,
  0x043b, 0x0446, 0x0451, 0x045c, 0x0467, 0x0472, 0x047e, 0x0489, 0x0494, 0x04a0, 0x04ab, 0x04b7, 0x04c2, 0x04cd, 0x04d9, 0x04e5,
  0x04f0, 0x04fc, 0x0507, 0x0513, 0x051f, 0x052b, 0x0536, 0x0542, 0x054e, 0x055a, 0x0566, 0x0571, 0x057d, 0x0589, 0x0595, 0x05a1,
  0x05ad, 0x05b9, 0x05c5, 0x05d1, 0x05de, 0x05ea, 0x05f6, 0x0602, 0x060e, 0x061a, 0x0627, 0x0633, 0x063f, 0x064b, 0x0658, 0x0664,
  0x0670, 0x067c, 0x0689, 0x0695, 0x06a2, 0x06ae, 0x06ba, 0x06c7, 0x06d3, 0x06e0, 0x06ec, 0x06f8, 0x0705, 0x0711, 0x071e, 0x072a,
  0x0737, 0x0743, 0x0750, 0x075c, 0x0769, 0x0775, 0x0782, 0x078f, 0x079b, 0x07a8, 0x07b4, 0x07c1, 0x07cd, 0x07da, 0x07e6, 0x07f3,
};

// send a word to the DAC
void sendDacWord(uint16_t word)
{
  digitalWrite(csPin, LOW);
  SPI.transfer(word >> 8);
  SPI.transfer(word & 0xff);
  digitalWrite(csPin, HIGH);
}

void setup()
{
  // init pins
  pinMode(ledPin, OUTPUT);
  pinMode(csPin, OUTPUT);
  digitalWrite(csPin, HIGH);

  // init SPI
  SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
  SPI.begin();  // additional begin is required, otherwise the second call to SPI.transfer hangs

  // send wakeup command to DAC
  sendDacWord(0xf000);
 
  // initialize timer1
  noInterrupts(); // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;
  OCR1A = 16000000.0f / samplerate; // compare match register for IRQ with selected samplerate
  TCCR1B |= (1 << WGM12); // CTC mode
  TCCR1B |= (1 << CS10); // no prescaler
  TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
  interrupts(); // enable all interrupts
}

// timer 1 interrupt
ISR(TIMER1_COMPA_vect)
{
  sendDacWord(nextValue & 0x0fff);
  nextValue = sinTable[sinIndex >> 22];
  sinIndex += increment;
}

void loop()
{
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  delay(500);
}

Code to generate the sin table:

Code: [Select]
#!/usr/bin/python

import math
import sys

length = 1024

for i in xrange(length):
    s = math.sin(2 * math.pi * i / length) * 2047 + 2048
    sys.stdout.write("0x" + format(int(s), '04x') + ", ")
    sys.stdout.flush()
    if i % 16 == 15: print("")
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 
The following users thanked this post: HoracioDos

Offline HoracioDosTopic starter

  • Frequent Contributor
  • **
  • Posts: 344
  • Country: ar
  • Just an IT monkey with a DSO
Re: Best way to amplify a sine wave without distorsion.
« Reply #34 on: January 28, 2017, 08:17:17 am »
Cool! I read something like this some time ago.

But the interrupt uses timer 1, which means you can't use the Arduino PWM functions anymore.

I guess an 8 bit fast pwm could be still available on timer 0. (Uno, Mega, etc)
 

Offline HoracioDosTopic starter

  • Frequent Contributor
  • **
  • Posts: 344
  • Country: ar
  • Just an IT monkey with a DSO
Re: Best way to amplify a sine wave without distorsion.
« Reply #35 on: January 28, 2017, 11:07:35 am »
It seems that the easiest change could be replacing R4 with a 5V light bulb (light bulbs are chasing me after all) and R3 with a 100K trimpot.
You will not find a light bulb with a warm resistance as high as 47k ohms so the value of R3 must be reduced a lot. You probably know that the opamp gain for a Wien bridge oscillator must be about 3. Also you need to use a power amplifier to drive the light bulb instead of an opamp.

I didn't know the resistance values for a light bulb. According to this, it's 115 ohms @ 5V. As the gain should be equal or greater than 3. R3 should not be greater than 200 ohms, that's why R4 is 47K? almost 1/2 of R3 with 100 Kohms?. Is that correct? So 100 Kohms is too high. I didn't know that an Op-Amp can't drive a ligh bulb.


I have many 0.1uF (104) capacitors. I guess It could work.
Don't guess, look at the input resistance of 50k ohms for the LM386 in its datasheet then calculate that a 0.1uF capacitor will pass frequencies above about 32Hz.

It should be about 0.0033uF for a 964hz frequency. I feel I'm at school again. is it correct?
« Last Edit: January 28, 2017, 11:11:50 am by HoracioDos »
 

Offline HoracioDosTopic starter

  • Frequent Contributor
  • **
  • Posts: 344
  • Country: ar
  • Just an IT monkey with a DSO
Re: Best way to amplify a sine wave without distorsion.
« Reply #36 on: January 28, 2017, 11:37:31 am »
The LM386 can be used as a Wein bridge oscillator with a J-FET AGC circuit, rather than the light bulb suggested on the data sheet.



So options are:

1) Use LM358 and AGC with a JFET
2) Use LM386 and AGC with a light bulb
3) Use LM386 and AGC with a JFET
4) Use MAX5712.

I like them all and would like to build them for a learning aim but option 3 is quite tempting to finish this project. I can't see the RC network so clearly to calculate it's frequency but everything between 800-1200 Hz is fine. Does it require something else like an attenuator?
 

Offline janoc

  • Super Contributor
  • ***
  • Posts: 3785
  • Country: de
Re: Best way to amplify a sine wave without distorsion.
« Reply #37 on: January 28, 2017, 11:44:42 am »
Hmm,

Am I the only one who has noticed this in the OPs post (emphasis mine)?

Quote
I want to amplify it to feed a VHF handheld radio mic with a minimum distorsion

Something like a LM386 is an enormous overkill there that will overdrive the radio. More likely what you need is a simple buffer for the oscillator/DAC/whatever you use to generate the signal and then an attenuator (= voltage divider). Microphone inputs take very small signals (in the order of milivolts), because there is an amplifier right there.

 
The following users thanked this post: HoracioDos

Offline Richard Crowley

  • Super Contributor
  • ***
  • Posts: 4317
  • Country: us
  • KJ7YLK
Re: Best way to amplify a sine wave without distorsion.
« Reply #38 on: January 28, 2017, 12:23:59 pm »
Yes. If the OP needs only microphone-level, he doesn't need ANY kind of amplification at all.  If he has a 5V square wave (or even a 3.3V square wave), he can easily make a sharp (High-Q) filter to create a nice sine wave for a fixed frequency.  And even then likely require an attenuator (like a simple pot) to create mic-level.

OTOH, he also mentioned driving a speaker which was acoustically-coupled (?) to the transmitter microphone. Although that logic-level signal would likely drive a small dynamic speaker transducer.

Ham radio forums and websites are rich sources of exactly this kind of tone-generator.  This is a rather common project an the amount of pathos and drama here seems overwrought for such a simple task.
 

Offline HoracioDosTopic starter

  • Frequent Contributor
  • **
  • Posts: 344
  • Country: ar
  • Just an IT monkey with a DSO
Re: Best way to amplify a sine wave without distorsion.
« Reply #39 on: January 28, 2017, 01:30:58 pm »
Ham radio forums and websites are rich sources of exactly this kind of tone-generator. 

I agree, there are a lot of circuits out there but not everything works as it is said. I attached a design in a previous post from QST Magazine for a Modulated CW keyer with transistors but It has a lot of distortion. I have another "proven" design with too many components, a lot more than the current circuit in discussion, are them really necesary?

Little example: Last month I've built two input/output radio interfaces (one for a yaesu and the other for a baofeng). I'm sure you know them (very easy project). Internet is full of them. They have a lot of low pass filters that are not needed or it's values doesn't make sense at all and in many cases are very risky. I made my own design with the components that are really necesary and they work just fine.

Google is not heaven.

This is a rather common project an the amount of pathos and drama here seems overwrought for such a simple task.

I love drama I'm a latin. It makes people to be humans.
 

Offline Audioguru

  • Super Contributor
  • ***
  • Posts: 1507
  • Country: ca
Re: Best way to amplify a sine wave without distorsion.
« Reply #40 on: January 28, 2017, 03:22:02 pm »
I didn't know that an Op-Amp can't drive a ligh bulb.
5V/115 ohms is an average current of 44mA and a peak of 62mA. The datasheet of the opamp tells you that its maximum output current is much less.

Quote
It should be about 0.0033uF for a 964hz frequency.
3.3nF into the 50k input of an LM386 produces a cutoff frequency of about 970Hz (I round it off a little) but then the fundamental frequency level is reduced to 0.707 times but the distortion frequencies remain at full level. Use a capacitor value 5 times more for no attenuation.
 
The following users thanked this post: HoracioDos

Online Zero999

  • Super Contributor
  • ***
  • Posts: 19514
  • Country: gb
  • 0999
Re: Best way to amplify a sine wave without distorsion.
« Reply #41 on: January 28, 2017, 05:27:47 pm »
The datasheet for the LM386 says that it becomes unstable if its gain is reduced to less than 9  but here it is 3. Do you think the capacitor between pin 1 and pin 8 reduces its high frequencies enough to make it stable with a gain of only 3?

I got the circuit off the datasheet but modified it, swapping the lamp for a J-FET. Unfortunately I made a mistake. I missed that the frequency determining resistors are different: 4k7 & 47k, so the gain needs to be much higher than 3 and the resonant frequency is 1kHz with C1 & C2 = 10nF and R1 = 4k7 & R2 = 47k.
 
The following users thanked this post: HoracioDos

Offline orolo

  • Frequent Contributor
  • **
  • Posts: 352
  • Country: es
Re: Best way to amplify a sine wave without distorsion.
« Reply #42 on: January 28, 2017, 06:55:15 pm »
Last week I built a Wien bridge oscillator with an LM358 and a JKL 7210, miniature 5V 30mA light builb. It worked, giving a very clean sinewave. I think I still have the protoboard somewhere in the lab. I used a pot to set the gain; the circuit oscillates a little below the bulb's threshold of visibility. After building the circuit I thought it was doubtful anyone would care about that circuit, and just a week later there is a post about that very same thing! BTW, unless you want a very low distortion sinewave, why don't you try some of the many alternatives? In this page there is a great discussion about Wien bridge oscillators. I found they use far stouter bulbs than I used.
 
The following users thanked this post: HoracioDos

Offline SeanB

  • Super Contributor
  • ***
  • Posts: 16281
  • Country: za
Re: Best way to amplify a sine wave without distorsion.
« Reply #43 on: January 28, 2017, 08:10:14 pm »
Built one years ago, and, while I do actually have a RA53 or two around somewhere, I used a small 28VDC wire ended grain of wheat bulb as a control element. 28V 20mA IIRC, and it runs at a non glowing voltage, but still enough to give a good control of level in the oscillator.
 
The following users thanked this post: HoracioDos

Online Zero999

  • Super Contributor
  • ***
  • Posts: 19514
  • Country: gb
  • 0999
Re: Best way to amplify a sine wave without distorsion.
« Reply #44 on: January 28, 2017, 10:28:39 pm »
It seems that the easiest change could be replacing R4 with a 5V light bulb (light bulbs are chasing me after all) and R3 with a 100K trimpot.
You will not find a light bulb with a warm resistance as high as 47k ohms so the value of R3 must be reduced a lot. You probably know that the opamp gain for a Wien bridge oscillator must be about 3. Also you need to use a power amplifier to drive the light bulb instead of an opamp.

I didn't know the resistance values for a light bulb. According to this, it's 115 ohms @ 5V. As the gain should be equal or greater than 3. R3 should not be greater than 200 ohms, that's why R4 is 47K? almost 1/2 of R3 with 100 Kohms?. Is that correct? So 100 Kohms is too high. I didn't know that an Op-Amp can't drive a ligh bulb.
Using the correct circuit on the data sheet. The value of the bulb's resistance can be calculated.

See the formulae on Wikipedia:
https://en.wikipedia.org/wiki/Wien_bridge_oscillator#Wien_bridge

And compare the schematic to the one on the LM386 data sheet.






C1 = C2 = 10nF = 10×10-9
R1 = 4k7 = 4700
R2 = 47k = 47×103

Rf/Rb = 10×10-9/10×10-9 + 47×103/4700 = 1 + 10 = 11

The bulb is Rb and the 390R resistor Rf.

Therefore:
Rb = 390/11 = 35.5R

In real life RB needs to be slightly less because the amplifier has an open loop gain of much less than infinity: 200 in the LM386's case.

Quote
I have many 0.1uF (104) capacitors. I guess It could work.
Don't guess, look at the input resistance of 50k ohms for the LM386 in its datasheet then calculate that a 0.1uF capacitor will pass frequencies above about 32Hz.

It should be about 0.0033uF for a 964hz frequency. I feel I'm at school again. is it correct?



F = 1/(2pi(C1×C2×R1×R2)½)

F  = 1/(2pi(10×10-9×10×10-9×4700×R2 = 47k = 47×103)½) = 1.07kHz

Here's the correct version with the J-FET, based on the data sheet schematic.


If anyone is interested, I'll post the LTSpice file and LM386 model.
« Last Edit: January 29, 2017, 11:08:21 pm by Hero999 »
 
The following users thanked this post: HoracioDos

Offline Audioguru

  • Super Contributor
  • ***
  • Posts: 1507
  • Country: ca
Re: Best way to amplify a sine wave without distorsion.
« Reply #45 on: January 29, 2017, 06:28:57 pm »
Shouldn't the RC frequency of the lowpass be the same as the RC frequency of the highpass?
 
The following users thanked this post: HoracioDos

Online Zero999

  • Super Contributor
  • ***
  • Posts: 19514
  • Country: gb
  • 0999
Re: Best way to amplify a sine wave without distorsion.
« Reply #46 on: January 29, 2017, 11:07:26 pm »
Shouldn't the RC frequency of the lowpass be the same as the RC frequency of the highpass?
No. The cut-off frequencies of both RC circuits don't have to be the same. They normally are the same because it makes the calculations and designing the circuit easier. If the two RC circuits are different, then it will still work but the attenuation factor at the resonant frequency is no longer 3.
 
The following users thanked this post: HoracioDos

Offline HoracioDosTopic starter

  • Frequent Contributor
  • **
  • Posts: 344
  • Country: ar
  • Just an IT monkey with a DSO
Re: Best way to amplify a sine wave without distorsion.
« Reply #47 on: January 30, 2017, 07:50:13 pm »
If anyone is interested, I'll post the LTSpice file and LM386 model.

Thank you very much for the schematic and the calculations!! Yes! I'm interested in the spice model. You've done a big effort helping me so I would like to learn as much as I can from it.

I've uploaded the Kicad schematic to give something back to the community.
By the way, this is the first time I used Kicad so it might have some error. If someone downloads it, please check it against the spice picture.
« Last Edit: January 30, 2017, 07:55:13 pm by HoracioDos »
 

Online Zero999

  • Super Contributor
  • ***
  • Posts: 19514
  • Country: gb
  • 0999
Re: Best way to amplify a sine wave without distorsion.
« Reply #48 on: January 30, 2017, 11:43:17 pm »
I don't have KiCAD installed on this PC at the moment, so can't view that file now. I'll do tomorrow.

Attached is the LTSpice (if you don't have it, it's a fee download) file for the circuit I posted previously. I've embedded the model for the LM386. Unfortunately, there doesn't seem to be any way to embed the symbol I used for the LM386, so I had to use the 8-pin package which should come with LTSpice. I've labelled the pins, in the hope it makes it more clear.
 

Offline HoracioDosTopic starter

  • Frequent Contributor
  • **
  • Posts: 344
  • Country: ar
  • Just an IT monkey with a DSO
Re: Best way to amplify a sine wave without distorsion.
« Reply #49 on: January 31, 2017, 12:09:47 pm »
Attached is the LTSpice (if you don't have it, it's a fee download) file for the circuit I posted previously. I've embedded the model for the LM386. Unfortunately, there doesn't seem to be any way to embed the symbol I used for the LM386, so I had to use the 8-pin package which should come with LTSpice. I've labelled the pins, in the hope it makes it more clear.

I installed LTspice XVII(x64) under wine and it works fine. This is the Vout graph.
As far as I can understand, you had to recreate the LM386 internals in spice! What a huge job!

As soon as I test the whole project in the breadboard I'll upload the schematics and PCB. The circuit will also have an atmega328p, on-off switching transistors, a voltage regulator and some other stuff. As I've done all this things before there will be no problems.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf