Re: Generating a rising sine tone with sinf()
Re: Generating a rising sine tone with sinf()
- Subject: Re: Generating a rising sine tone with sinf()
- From: Andreas Tell <email@hidden>
- Date: Thu, 19 Jun 2014 18:51:10 +0200
Hi Jim,
you fell into a very common trap when it comes to synthesise sines (or other waveforms) with non-constant frequency. The relationship s(t) = sin( 2*pi*f*t ) for the signal s(t) only holds for time constant f. The more general relationship is s(t) = sin( phase(t) ) where the time derivative d/dt phase = 2*pi*f(t). So if you have a frequency function f(t) you can calculate the phase function by integrating the frequency function with respect to t and multiplying it by 2 pi. For a linear frequency relationship like f(t) = a*t you get phase(t) = 2*pi * 1/2 a t^2 and plugging that into the sine gives you s(t) = sin( pi a t^2 ), which is why you found that replacing 2 pi with pi fixed your problem.
Cheers,
Andy
On 19 Jun 2014, at 18:42, Jim McGowan <email@hidden> wrote:
> I’m writing some code to generate a sine tone that begins at a given frequency and the frequency rises linearly to a maximum over a specified time. Seemingly straightforward, my approach was to fill a sample buffer in a loop (I’m using the AudioSampleType on OS X, i.e. Float32):
>
> frequency = minFreq + (frequencyRangeLength * (sampleIndex / lengthInSamples));
> sineSweepSamples[sampleIndex] = sinf((2.0 * M_PI * sampleIndex * frequency) / sampleRate)
>
> However, this generates a tone where the frequency increases at a faster rate than it should. When I remove the “2.0 *”, i.e. use pi instead of 2pi, I get the results I want, but I don’t understand why. Checking the formula (with the 2pi) in the Grapher app shows it is correct, and by dumping all the values in each iteration of the loop I see that the frequency is being calculated properly.
>
> Can anyone explain this for me?
>
> Thanks,
> Jim
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Coreaudio-api mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden