Converting audio square wave to sine using microcontroller

Thread Starter

rfpd

Joined Jul 6, 2016
101
I tried doing this using low pass filters but didn't manage for it to work properly, so I found out that I could use a microcontroller to track the square wave period and output a sine wave with the same period. My question is, would I still need to use a flip flop, dividing the frequency for it to track easier, or can I connect the sine wave diretly? Is arduino fit for this? Or is there a better microcontroller for this?
 

OBW0549

Joined Mar 2, 2015
3,566
I tried doing this using low pass filters but didn't manage for it to work properly, so I found out that I could use a microcontroller to track the square wave period and output a sine wave with the same period. My question is, would I still need to use a flip flop, dividing the frequency for it to track easier, or can I connect the sine wave diretly? Is arduino fit for this? Or is there a better microcontroller for this?
How do you propose to generate a sine wave using a microcontroller?
 

crutschow

Joined Mar 14, 2008
34,280
You could conceivably use a sine lookup table with a D/A output to generate a sinewave with a micro.
You do realize that the output of this will have a fixed amplitude.
 

Thread Starter

rfpd

Joined Jul 6, 2016
101
You could conceivably use a sine lookup table with a D/A output to generate a sinewave with a micro.
You do realize that the output of this will have a fixed amplitude.
That s no problem, as long as every frequency is clean and with the same amplitude, after that I could amplify or attenuate it. I'll look into it, thanks.

How do you propose to generate a sine wave using a microcontroller?
I don't know, I saw it in a post here, I was asking if it was possible and how.
 

OBW0549

Joined Mar 2, 2015
3,566
I don't know, I saw it in a post here, I was asking if it was possible and how.
I would do what @crutschow suggested; the technique is called Direct Digital Synthesis, or DDS. Googling the term will get you plenty of information.

A few years back I built myself a little DDS sine wave signal generator using a dsPIC30F3013 and an LT8043 12-bit DAC, covering the frequency range 0 Hz to 33 kHz. It worked really well.
 

Gibson486

Joined Jul 20, 2012
355
I am a little confused. You tried to take a square wave and filter to become a sine wave? It is possible to do that, but it would require two filter stages....

You could also do it with the micro solution. Not sure how you would do that in real time though. However, as mentioned above, DDS chips do this, but I am not sure how one would be able to track and output at the same time. If you do not mind a minor delay, then yes, it is feasible.
 

DickCappels

Joined Aug 21, 2008
10,153
(Some text removed for clarity.) My question is, would I still need to use a flip flop, dividing the frequency for it to track easier, or can I connect the sine wave directly? Is Arduino fit for this? Or is there a better microcontroller for this?
You question confuses me. The picture I have is a square wave going into the controller and a sine wave coming out. If you are going to make a phse-locked loop that uses a digital phase comparator (but why when you have a sine wave?) you would want to square up the sine wave by passing it through an analog comparator possibly with a little hysteresis before feeding it to the phase comparator.

Most modern micrcontrollers should be able to handle the task easily.

You have not provided any indication of the amplitude stability and purity you require or the response time (time to change frequency). For example, is a glitch while the frequency is changed acceptable?
 

Alec_t

Joined Sep 17, 2013
14,280
A glitch seems inevitable, since the micro will need at least half a wave period to make up its mind what the period duration is, or to decide that the period has changed.
 

Thread Starter

rfpd

Joined Jul 6, 2016
101
You question confuses me. The picture I have is a square wave going into the controller and a sine wave coming out. If you are going to make a phse-locked loop that uses a digital phase comparator (but why when you have a sine wave?) you would want to square up the sine wave by passing it through an analog comparator possibly with a little hysteresis before feeding it to the phase comparator.

Most modern micrcontrollers should be able to handle the task easily.

You have not provided any indication of the amplitude stability and purity you require or the response time (time to change frequency). For example, is a glitch while the frequency is changed acceptable?
The main goal is dividing a guitar note's frequency, I'm trying to clean it up after the flip flop. I was going to talk about the response but I couldn't edit, should be in the order of low msecs, not sure enough, it's the time it takes for someone to play another note.

Since I don't have a microcontroller here with me, and if you don't mind me asking, I was looking into different solutions, the low pass filters worked (badly for the thicker string), but I wanted more quality so I was trying to use an integrator montage. Not working though, it seemed like it was doing nothing.

upload_2017-7-29_23-34-16.png

I tried this, and I also connected the flip flop output to the integrator montage input, in the simulation it works, in reality it doesn't (fuzzy sound, even tried a low pass filter just to make sure, and I don't think it was working properly).

My question is, in this montage should the input be biased (ex: 9 V,0V or 4V, 3V) or should it be centered (ex: 1.5V, -1.5V). Since it's connected to ground it would clip the negative part, right?

EDIT: I managed for it work with a 3 stage low pass filter, I don't get it, sometimes it works well, sometimes it doesn't, I'll do further tests tomorrow. Thanks for the help, still if you could answer why this wasn't working, I'll appreciate it, thanks!
 
Last edited:

ScottWang

Joined Aug 23, 2012
7,397
You could using two stages of RC filters(integral circuit) to convert the square wave to sine wave, but the voltage will be become smaller, so you have to using one or two stages of op amp to amplifying the sine wave signal, you can using the VR to adjust the wave form of sine wave and choosing the properly values of VR and capacitors to do the filtering job.
 

Alec_t

Joined Sep 17, 2013
14,280
Since it's connected to ground it would clip the negative part, right?
Yes. That probably accounts for the fuzzy sound. Also, if you really are using a 741 you should check the datasheet. You will see the limitations on the input signal range it can accept. Its gain-bandwidth product is alo pretty poor so you'd lose higher frequencies. If you use the 741 with a single-polarity supply then you should bias the inputs at about half the supply voltage.
 

bertus

Joined Apr 5, 2008
22,270
Hello,

As said, the audio amplitude will change with the frequency, when you use the integrators.
You could try a setup like this to avoid this problem:

rfpd_audio_divider.png
The output signal will follow the amplitude changes of the input signal when correct configured.

Bertus
 
Top