How to change the harmonics of a sinwave

Tags:
1. Nov 30, 2014

btb4198

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. Nov 30, 2014

Staff: Mentor

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

3. Dec 1, 2014

Baluncore

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.

4. Dec 1, 2014

btb4198

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?

File size:
3.8 KB
Views:
116
5. Dec 1, 2014

btb4198

I am using 44100 Hz for my sampling rate. I am not recirculating the buffer and I generating new values during run time.

File size:
3.8 KB
Views:
120
6. Dec 1, 2014

f95toli

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.

7. Dec 1, 2014

btb4198

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

8. Dec 1, 2014

btb4198

I added a square wave Modulation to it , but it does not sound better ...
what would a filter do ? that only remove Frequencies right ?

9. Dec 1, 2014

btb4198

Ok i added a saw tooth wave with with Frequency * 4 and now it just sound annoying

10. Dec 1, 2014

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.

11. Dec 1, 2014

Baluncore

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.

12. Dec 1, 2014

Baluncore

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.

13. Dec 1, 2014

btb4198

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

14. Dec 1, 2014

btb4198

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

15. Dec 1, 2014

btb4198

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

16. Dec 1, 2014

Baluncore

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.

17. Dec 1, 2014

btb4198

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 ?

18. Dec 1, 2014

btb4198

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 ?

19. Dec 1, 2014

btb4198

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.

20. Dec 2, 2014

davenn

its peak, not peek

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

21. Dec 2, 2014

davenn

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

22. Dec 2, 2014

Baluncore

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 ?

23. Dec 2, 2014

btb4198

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

24. Dec 2, 2014

btb4198

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 ...

25. Dec 2, 2014

Baluncore

No, you have two interleaved channels.