Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to change the harmonics of a sinwave

  1. Nov 30, 2014 #1
    how do I to change the harmonic of a sin wave ? I want it to sound more like a real note.
    I tried this :
    buffer[n] = Amplitude * Math.Sin(Math.PI * Frequency * n / 44100D) + (Amplitude/2 * Math.Sin(Math.PI * Frequency/2 * n / 44100D)) + (Amplitude/2 * Math.Sin(Math.PI * Frequency *2 * n / 44100D)));

    but the sound has a beat ... I was told that is you times the Frequency be two or dived the Frequency by two it was not have a beat, but that is not right
    is there another way?
  2. jcsd
  3. Nov 30, 2014 #2


    User Avatar

    Staff: Mentor

    Try just adding in higher frequencies, these are the harmonics, 2f, 3f, etc., with f being the fundamental. See how that sounds.
  4. Dec 1, 2014 #3


    User Avatar
    Science Advisor

    More like a "note" from what instrument? A pure sine wave is a pure note without harmonics.

    You are adding half amplitude f/2 and 2f to a full amplitude f. That makes a fundamental at f/2 with an amplitude = 0.5, with a second harmonic at f of amplitude = 1.0, and a fourth harmonic at 2f with amplitude 0.5

    What is the length of buffer that you are using? What is your data rate, 44100 Hz?
    You are dealing with digitally generated sounds so, if you are recirculating the generated buffer, you should make the length of your buffer a multiple of the period of all frequencies you synthesis into that buffer. Without that you will have a steep transient step between the ends of the buffer which will generate high frequency noise.
  5. Dec 1, 2014 #4
    ok I did this :

    buffer[n] = ((Amplitude * Math.Sin(Math.PI * Frequency * n1 / 44100D)) + ((Amplitude / 2) * Math.Sin(Math.PI * (Frequency* 3) * n1 / 44100D)) + ((Amplitude / 2) * Math.Sin(Math.PI * (Frequency * 2) * n1 / 44100D)))

    when I play it there is no beats, but the sound still sound boring .
    Also I did an FFT and I set Frequency to 261.60
    I got this

    I got 263.7817 is the 1st peek
    522.18 is the second peek
    785.96 is the third peek.
    ok why are 522.18 Hz and 785.96 Hz higher than 263.78?

    Attached Files:

  6. Dec 1, 2014 #5
    I am using 44100 Hz for my sampling rate. I am not recirculating the buffer and I generating new values during run time.

    Attached Files:

  7. Dec 1, 2014 #6


    User Avatar
    Science Advisor
    Gold Member

    What do you trying to get? A sine wave is always going to sound "boring" even if you add a couple of harmonics. If you are trying to get a sound akin to what old style synthesizers (or even a SID chip) would generate you need more than just a sine wave and some harmonics; they used all sorts of tricks to generate interesting sound (filters, modulation etc)

    Why don't you try to play with a software synth (I am sure there are some free ones). That should give you some idea of what to expect to hear.
  8. Dec 1, 2014 #7
    I set Frequency to 370 Hz
    I got this:

    I got 371 Hz is the 1st peek
    737.51 Hz is the second peek
    1108.9599 Hz is the third peek.
    so this time 1108.95 is the highest point
    and why not 371Hz?

    I do (Amplitude * Math.Sin(Math.PI * Frequency * n1 / 44100D))
    the other two have Amplitude/2
    oh I set Amplitude to 1
  9. Dec 1, 2014 #8
    I added a square wave Modulation to it , but it does not sound better ...
    what would a filter do ? that only remove Frequencies right ?
  10. Dec 1, 2014 #9
    Ok i added a saw tooth wave with with Frequency * 4 and now it just sound annoying
  11. Dec 1, 2014 #10


    User Avatar

    Staff: Mentor

    A continuous tone will sound boring, like that of a single long note of an organ or bagpipes. Most musical instruments have frequency & amplitude distortion at the start and end of each note to give their characteristic recognizable sound, e.g., a piano where the amplitude builds explosively then quickly decays.

    I don't know why your FFT is not as expected. Try moving the fundamental up to 1kHz and one at a time add harmonics, see how that goes. There might be something upsetting the lower frequencies, but I can't imagine what it might be.
  12. Dec 1, 2014 #11


    User Avatar
    Science Advisor

    You are adjusting too many things at the time. Graph your buffer contents so we can see what your wave looks like.
    You might be clipping due to high amplitude, or you might be getting a step between generating buffers by always starting again at zero time.
  13. Dec 1, 2014 #12


    User Avatar
    Science Advisor

    You have forgotten the 2 in Sin( 2 * Pi * frequency * n / 44100 )

    The spectrum you show at the top of post #7 is a Dirac comb function. It is it's own fourier transform.
    It is the spectrum of a sawtooth or a repeated narrow pulse with a repetition rate of 370 Hz.

    If you do too much at once we cannot help you. Specify all information once in one place.
    1. Write out your generator synthesis equation including the for … next loop that controls it.
    2. Then plot your waveform to see what you actually get.
    3. FFT to the spectrum. If it is different to the synthesis then you have a numerical bug in your code.
  14. Dec 1, 2014 #13
    ok I went back down to this :
    buffer[n] = (float)((Amplitude * Math.Sin(Math.PI * Frequency * n1 / 44100D)) + ((Amplitude / 2) * Math.Sin(Math.PI * (Frequency * 3) * n1 / 44100D)) + ((Amplitude / 2) * Math.Sin(Math.PI * (Frequency * 2) * n1 / 44100D)));

    Frequency = 622.3 Hz
    Amplitude = 0.6

    This is what I have :

    and this is the FFT:

    peak 1) 624.46 Hz
    peak 2) 1243.45 Hz
    peak 3) 1868.005 Hz

    Ok that one works because the Frequency = 622.3 Hz, however I did think that 1243.45 Hz and 1868.005 Hz would have the same peak
  15. Dec 1, 2014 #14
    I set Frequency = 87.31 Hz and Amplitude = 3
    and I got this :
    and this is the fft:
    yeah that is so wrong :
    peak one ) 172.26 Hz
    peak two) 263.7817 Hz the sec peak it really high
    Peak 3) 436.04 Hz
    Peak 4) 522.1801 Hz
  16. Dec 1, 2014 #15
    ok I set Frequency = 311.1Hz and Amplitude = 1 and I got this :

    and the FFT:
    Peek 1)312.231 Hz
    Peek 2)624.462 Hz
    Peek 3)931.3110 Hz
    ok, why is Peek 2 a lot bigger than peek 1 ? Peek 1 is the fundamental frequency, not peek 2
  17. Dec 1, 2014 #16


    User Avatar
    Science Advisor

    The flat tops and bottoms of your synthesised waveforms are clipping due to something in the code.
    Clipping generates harmonics. You need to reduce the amplitude to make the signal linear.
  18. Dec 1, 2014 #17
    so you are saying something is wrong with this
    uffer[n] = (float)((Amplitude * Math.Sin(Math.PI * Frequency * n1 / 44100D)) + ((Amplitude / 2) * Math.Sin(Math.PI * (Frequency * 3) * n1 / 44100D)) + ((Amplitude / 2) * Math.Sin(Math.PI * (Frequency * 2) * n1 / 44100D)));

    also just so you know,
    my Fs is 44100 Hz
    I am playing the sound from my computer and listening to it with my computer's mic.
    the sound it played using 2 channels and that is why I do not have Pi *2

    so what could be clipping?
    I am using 1 for my Amplitude, do you think that is too high ?
  19. Dec 1, 2014 #18
    ok i did Frequency = 80 Hz and Amplitude = 0.25 and I got this :
    and this for the fft:


    Peek 1)53.833Hz
    Peek 2)161.4990 Hz
    Peek 3)242.2485 Hz

    it got peek 2 and 3 right but one is wrong and one it should be 80Hz and that is the one that should be the biggest right ?
  20. Dec 1, 2014 #19
    ok i did Frequency = 280 Hz and Amplitude = 0.25 and I got this :
    the FFT:

    Peek 1)279.93Hz
    Peek 2)559.863 Hz
    Peek 3)839.794Hz

    ok why is Peek one so low and peek 2 so high ?
    in my equation Frequency is the only one that Amplitude is not dived my 2
    Frequency * 2
    Frequency *3
    both have Amplitude/2
    so is Frequency *2 really the fundamental frequency? if so why?
    I really thought it Frequency because it has the highest Amplitude out the 3.
  21. Dec 2, 2014 #20


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    its peak, not peek

    you might be overdriving the microphone or its input on the sound card resulting in clipping ?

    since you haven't really explained your setup, its quite difficult to offer helpful advice
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: How to change the harmonics of a sinwave