How to change the harmonics of a sinwave

In summary: I am using 44100 Hz for my sampling rate. I am not recirculating the buffer and I generating new values during run time.I tried 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
  • #1
btb4198
572
10
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?
 
Engineering news on Phys.org
  • #2
Try just adding in higher frequencies, these are the harmonics, 2f, 3f, etc., with f being the fundamental. See how that sounds.
 
  • #3
btb4198 said:
I want it to sound more like a real note.
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
NascentOxygen said:
Try just adding in higher frequencies, these are the harmonics, 2f, 3f, etc., with f being the fundamental. See how that sounds.

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

Attachments

  • upload_2014-12-1_10-32-11.png
    upload_2014-12-1_10-32-11.png
    3.8 KB · Views: 463
  • #5
Baluncore said:
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.

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

Attachments

  • upload_2014-12-1_10-32-11.png
    upload_2014-12-1_10-32-11.png
    3.8 KB · Views: 467
  • #6
btb4198 said:
when I play it there is no beats, but the sound still sound boring .

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
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
 
  • #8
f95toli said:
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.

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
Ok i added a saw tooth wave with with Frequency * 4 and now it just sound annoying
 
  • #10
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
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
btb4198 said:
I do (Amplitude * Math.Sin(Math.PI * Frequency * n1 / 44100D))
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
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
 
  • #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
 
  • #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
 
  • #16
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
Baluncore said:
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.

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
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 ?
 
  • #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.
 
  • #20
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
 
  • #21
btb4198 said:
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

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
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
davenn said:
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

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 ?
 
  • #24
Baluncore said:
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 ?

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
btb4198 said:
no, I am not using 44100 samples where did you get that from ? I am using 16384 samples for my fft.
btb4198 said:
I am using 44100 Hz for my sampling rate.
No, you have two interleaved channels.
btb4198 said:
also just so you know,
my Fs is 44100 Hz
btb4198 said:
… ((Amplitude * Math.Sin(Math.PI * Frequency * n1 / 44100D)) + …
 
  • #26
Baluncore said:
No, you have two interleaved channels.

Sorry I am not understanding what you are saying. I am using 16384 samples for my FFT but my sampling rate or Fs is 44100 Hz.
The number of Samples used for a FFT is different from the sampling rate( Fs).
 
  • #28
oK new Test,
I am no long playing the sound. right now I am just testing the FFT. so in code I generated a buffer using this:
Code:
  for (int n = 0; n < 52920; n++)
            {
                double R = Peak3 * (Math.Sin((2D * Math.PI * n * Frequency3 / 44100D))) + Peak3 * (Math.Sin((2D * Math.PI * n * Frequency2 / 44100D))) + Peak3 * (Math.Sin((2D * Math.PI * n * Frequency1 / 44100D)));
                list[n] = R;
            }

Then I sent it to the FFT.

so I use:
Frequency3 = 261.6 Hz
Frequency2 = 523.2Hz
Frequency1 = 784.8 Hz
and I got this :
Code:
Hz                                        db
257.5                            4120.46666257201
258.333333333333      3038.98507682412
260                               25560.0876647108
260.833333333333      17707.5370115662
262.5                            6007.02992093821
520.833333333333      9412.07311822615
521.666666666667      30276.806382244
523.333333333333      5900.92735037233
780                               5373.28298829535
781.666666666667      3748.77612698566
782.5                            28906.6487214165
784.166666666667      5048.81733049545
785                               9041.0204981875
787.5                            4042.43121808062
792.5                            3130.94203912514

260 Hz is close to 261.6 Hz
521.67 is close to 523.2 Hz
782.5 is close to 784.8 Hz
they all seem to be off by a little over 1

do you think my FFT is wrong ?
also I do not display anything with a db low then 3000

what do you all think?
is something wrong with the FFT?
 
  • #29
is there any way in fix my FFT?
 
  • Like
Likes Baluncore
  • #30
You seem blind to the fundamentals needed before processing sampled data. You question and doubt your computer hardware and FFT code, but you should not be leaping to conclusions and blaming others for the failure of your code implementation. You should be testing your code as you gradually increase the complexity until you are certain you understand the principles, it's capabilities and limitations.

You need to understand the difference between indexing and counting, along with the Fourier transform and spectral analysis. http://en.wikipedia.org/wiki/Frequency_spectrum#Spectrum_analysis

You then need to understand window functions. http://en.wikipedia.org/wiki/Window_function so you realize why you should analyse the FFT of full cycles, not partial cycles.

Without understanding the fundamentals and having a proven and stable code platform, you will be unable to progress. I'm afraid that you are so confused about what is relevant that you cannot answer our questions. That makes it very difficult to help you.
 
  • #31
Baluncore said:
You seem blind to the fundamentals needed before processing sampled data. You question and doubt your computer hardware and FFT code, but you should not be leaping to conclusions and blaming others for the failure of your code implementation. You should be testing your code as you gradually increase the complexity until you are certain you understand the principles, it's capabilities and limitations.

You need to understand the difference between indexing and counting, along with the Fourier transform and spectral analysis. http://en.wikipedia.org/wiki/Frequency_spectrum#Spectrum_analysis

You then need to understand window functions. http://en.wikipedia.org/wiki/Window_function so you realize why you should analyse the FFT of full cycles, not partial cycles.

Without understanding the fundamentals and having a proven and stable code platform, you will be unable to progress. I'm afraid that you are so confused about what is relevant that you cannot answer our questions. That makes it very difficult to help you.
I answered all your questions... but I a new to this. The fft does seem to for work for most cases ... but it just seem to be too noisy which I do not understand. Also, about window functions, if you have a signal coming in and you do not know the Frequencies in the signal you can't really do a window function right ?
 
  • #32
btb4198 said:
Also, about window functions, if you have a signal coming in and you do not know the Frequencies in the signal you can't really do a window function right ?
Wrong.
 
  • #33
btb4198 said:
I answered all your questions... but I a new to this. The fft does seem to for work for most cases ... but it just seem to be too noisy which I do not understand. Also, about window functions, if you have a signal coming in and you do not know the Frequencies in the signal you can't really do a window function right ?
Why are you not taking his advice? You need to know far more about this than you seem to, in order to use a DFT process. Do you realize that the DFT assumes that the sequence you are analysing, actually repeats for ever? If you take an arbitrary number of samples from your signal then the result of a DFT will contain harmonics of the repeat rate, beating with the signal you have sampled. You are not in a position to complain that your FFT is 'broken' as you have no evidence that it is. You are just not using the tool correctly. You must learn more and stop complaining, I think.
 
  • #34
sophiecentaur said:
Why are you not taking his advice? You need to know far more about this than you seem to, in order to use a DFT process. Do you realize that the DFT assumes that the sequence you are analysing, actually repeats for ever? If you take an arbitrary number of samples from your signal then the result of a DFT will contain harmonics of the repeat rate, beating with the signal you have sampled. You are not in a position to complain that your FFT is 'broken' as you have no evidence that it is. You are just not using the tool correctly. You must learn more and stop complaining, I think.

I am looking into window functions. I watched a view you tube videos on them. From what I learned you zero out part of the signal, so when you do the FFT, you will have a complete cycle and not half a cycle nor part a cycle: This reduces leaking power into other bins. Am I right about this ?

What I do not understand is that when you zero out part of the signal you are changing it, and there for you are losing information, I would think that would be bad in communications... like cells phones they have DSP chips in them right ?

Also, using Audacity I played with the FFT and Hanning window function. I generate a pluck of middle C and with a sampling size of 16384 and Hanning window is seem to work good. not too noisy :

Untitled.png


right?
 
  • #35
If you are trying to analyses a "pluck" of middle C, you are not analysing a continuous waveform - the pluck is a transient and, by doing a DFT, you are assuming the pluck is repeated ad infinitum. Why would you want to do that? Surely the thing to do - to make sure you are getting the right result from what you are doing - is to produce a burst (widowed section) of pure sine wave at your wanted frequency and then check the DFT outcome is what you expect. One cannot run before one can walk. This is a topic that does not reveal much from a superficial approach.
 

Similar threads

Replies
18
Views
2K
  • Programming and Computer Science
Replies
21
Views
1K
Replies
15
Views
2K
  • Electrical Engineering
Replies
4
Views
936
  • Electrical Engineering
Replies
15
Views
4K
Replies
5
Views
3K
  • Electrical Engineering
Replies
15
Views
3K
  • Introductory Physics Homework Help
Replies
3
Views
351
Replies
5
Views
1K
Replies
2
Views
1K
Back
Top