Changing Sinewave Frequency: Can it be Done?

  • Thread starter Thread starter btb4198
  • Start date Start date
Click For Summary
The discussion centers on the challenge of generating a pure sine wave at a specific frequency using C#. The original poster is confused about receiving two frequencies instead of one and questions the use of Euler's formula in their calculations. Responses clarify that sine waves inherently have one frequency, while the output can range from negative to positive values due to the nature of trigonometric functions. Suggestions include simplifying the code to isolate the sine wave generation and addressing potential issues with abrupt volume changes that may cause unwanted artifacts in the sound. The conversation emphasizes understanding the mathematical principles behind sine waves and the importance of careful programming to achieve the desired audio output.
  • #31
btb4198 said:
I am not sure what you mean by "work out the answer 'properly"

What I meant was that the answer to your original question is obtainable in analytical form - giving an analytical expression that you could evaluate in one go and produce your output sample data. It strikes me that you are doing the processing the 'hard way' - and the code is letting you down. If you were dealing with an unspecified input then you would need to do it the 'DSP way' (as you are doing) but, as it's all internally generated, you should be able to calculate the final sample values in one straightforward evaluation.
How sure are you that the basic operation you are trying to do is valid and represents what you want to achieve?
 
Physics news on Phys.org
  • #32
This looks to me like Amplitude Modulation. Is there any more to it than that?
 
  • #33
OK, I found out what is going on. Your pulses are way too short in relation to the main frequency that you are modulating. I have filled in the absolute valued sine wave from the graphic that you provided in green to give you an idea.

why2.jpg


Do you need help with the math? Assuming (based on your code) that is your playback sample rate is 44100, and your playback frequency is 1000 hz, then you should find 1000 complete sine wave cycles for each second, or one sine wave every 44.1 samples. If you pulse that once per second you should get 500 complete cycles within each pulse.
 
  • #34
MikeGomez said:
OK, I found out what is going on. Your pulses are way too short in relation to the main frequency that you are modulating. I have filled in the absolute valued sine wave from the graphic that you provided in green to give you an idea.

View attachment 69918

Do you need help with the math? Assuming (based on your code) that is your playback sample rate is 44100, and your playback frequency is 1000 hz, then you should find 1000 complete sine wave cycles for each second, or one sine wave every 44.1 samples. If you pulse that once per second you should get 500 complete cycles within each pulse.

Yes I need help with the math.
also I just learned that my wave is all wrong. I have an program that picks up sound wave and does an FFT. I know that program works because I tested it with this site
http://onlinetonegenerator.com/?freq=5000

but when I play my tone generator the Freg are a little over 2 time what it should be.. why?

here is my code

Code:
        int sampleRate = WaveFormat.SampleRate;
        if (Beat == 0)
        {
            f2 = 0;
        }
        else
        {
            f2 = Frequency - Beat;
        }
        for (int n = 0; n < sampleCount; n++)
        {
            buffer[n + offset] = (float)(Amplitude * Math.Sin((2 * Math.PI * sample * Frequency) / sampleRate)) + (float)(Amplitude * Math.Sin((2 * Math.PI * sample * f2) / sampleRate));
            sample++;
            if (sample >= sampleRate) sample = 0;
        }
        buffer1 = buffer;
        return sampleCount;
    }
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 5 ·
Replies
5
Views
474
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 23 ·
Replies
23
Views
18K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
1K