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

    NascentOxygen

    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

    Baluncore

    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

    f95toli

    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:
    upload_2014-12-1_10-51-17.png


    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
    why?
    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

    NascentOxygen

    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

    Baluncore

    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

    Baluncore

    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 :
    upload_2014-12-1_15-9-26.png

    and this is the FFT:
    upload_2014-12-1_15-10-26.png

    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 :
    upload_2014-12-1_15-29-44.png
    and this is the fft:
    upload_2014-12-1_15-30-47.png
    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 :
    upload_2014-12-1_15-48-33.png

    and the FFT:
    upload_2014-12-1_15-49-22.png
    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

    Baluncore

    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 :
    upload_2014-12-1_22-35-0.png
    and this for the fft:

    upload_2014-12-1_22-34-0.png


    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 :
    upload_2014-12-1_22-44-14.png
    the FFT:
    upload_2014-12-1_22-44-38.png

    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

    davenn

    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
     
  22. Dec 2, 2014 #21

    davenn

    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    could be showing the better frequency response of the microphone at the second frequency

    again ... because you haven't told us much about your setup, its difficult to give good advice
     
  23. Dec 2, 2014 #22

    Baluncore

    User Avatar
    Science Advisor

    You should simplify your setup and first experiment with the amplitude effects of a sine wave only.
    You should show us the loop that synthesises the time domain signal, not just the inner function.

    Are you a masochist? Why are you adding harmonics when you have not yet got a clean predictable sine wave?
    Maybe you think that if you juggle your code fast enough it will all fall into place and suddenly work correctly.

    Re: Post #18. Why is there so much noise on your time plot? Where does that time data come from? Why are there multiple plots on your spectrum? Is it a power or an amplitude spectrum?

    The 44100 samples is not a power of two so you must be using an unusual FFT. If the FFT is a power of two then it will not be a multiple of signal periods and so will show noise due to an end wrap-around step. Are you windowing your data before the FFT ?
     
  24. Dec 2, 2014 #23
    What would you like to know about my set up ?
    I am using a Dell computer Interl(R) Core (TM) I7-2640M CPU @ 2.80 GHZ computer

    I am using a IDT High Definition Audio CODEC Microphone Array LR top Panal Internal ATAPI Jack
    I am using two channels and 48000 Hz, 16 bit for the Mic
    Microphone Array is set to 82 level
    Microphone Boost is + 20.0 dB Level

    For the Speakers is says
    Speakers/ Headphone
    IDT Hight Definition Audio CODEC

    Speakers level is 84

    16 bit, 48000 HZ(DVD Quality)
    any other information about the set up you might need ?
     
  25. Dec 2, 2014 #24
    Are you a masochist? no lol

    Why are you adding harmonics when you have not yet got a clean predictable sine wave?
    I did, A while back. My code works fine if I just have one sine ... but not so will with more than one : sine(3F) + sine(F)

    Why is there so much noise on your time plot?
    I do not know, I have a filter that does not display anything with a magnitude low than 300000, I am thinking because It is a dell computer

    Where does that time data come from?
    a program I wrote in C# that plays out my computer speakers

    Why are there multiple plots on your spectrum?
    I do not know, noise I guess

    The 44100 samples is not a power of two so you must be using an unusual FFT. If the FFT is a power of two then it will not be a multiple of signal periods and so will show noise due to an end wrap-around step.

    no, I am not using 44100 samples where did you get that from ? I am using 16384 samples for my fft.

    Are you windowing your data before the FFT ?
    kind of , i take 16384 at a time and do the FFT and i just low the other same until i redo the FFT so kind of ...
     
  26. Dec 2, 2014 #25

    Baluncore

    User Avatar
    Science Advisor

    No, you have two interleaved channels.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook