Converting audio square wave to sine using microcontroller

Discussion in 'General Electronics Chat' started by rfpd, Jul 28, 2017.

  1. rfpd

    Thread Starter Member

    Jul 6, 2016
    101
    0
    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?
     
  2. rfpd

    Thread Starter Member

    Jul 6, 2016
    101
    0
    Frequency is in the range of 40 to 500 Hz.
     
  3. OBW0549

    Distinguished Member

    Mar 2, 2015
    2,947
    2,704
    How do you propose to generate a sine wave using a microcontroller?
     
  4. crutschow

    Expert

    Mar 14, 2008
    23,093
    6,850
    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.
     
    rfpd likes this.
  5. rfpd

    Thread Starter Member

    Jul 6, 2016
    101
    0
    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.

    I don't know, I saw it in a post here, I was asking if it was possible and how.
     
  6. OBW0549

    Distinguished Member

    Mar 2, 2015
    2,947
    2,704
    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.
     
    rfpd likes this.
  7. Gibson486

    Active Member

    Jul 20, 2012
    306
    17
    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.
     
  8. DickCappels

    Moderator

    Aug 21, 2008
    5,746
    1,800
    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?
     
  9. Alec_t

    Expert

    Sep 17, 2013
    10,259
    2,508
    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.
     
  10. rfpd

    Thread Starter Member

    Jul 6, 2016
    101
    0
    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: Jul 29, 2017
  11. ScottWang

    Moderator

    Aug 23, 2012
    6,762
    1,047
    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.
     
  12. Alec_t

    Expert

    Sep 17, 2013
    10,259
    2,508
    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.
     
    rfpd likes this.
  13. bertus

    Administrator

    Apr 5, 2008
    19,908
    4,138
    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
     
    rfpd likes this.
Loading...