Generating a rising sine tone with sinf() (Adam Bellard)
Generating a rising sine tone with sinf() (Adam Bellard)
- Subject: Generating a rising sine tone with sinf() (Adam Bellard)
- From: E Hartley <email@hidden>
- Date: Fri, 20 Jun 2014 00:00:44 +0100
Hi
Look in math.h and the validity of sinf() in the 4 quadrants you're evaluating the function over. This is more usually associated with geometric problems.
HTH
Ed Hartley
> On 19 Jun 2014, at 20:00, email@hidden wrote:
>
> Send Coreaudio-api mailing list submissions to
> email@hidden
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://lists.apple.com/mailman/listinfo/coreaudio-api
> or, via email, send a message with subject or body 'help' to
> email@hidden
>
> You can reach the person managing the list at
> email@hidden
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Coreaudio-api digest..."
>
>
> Today's Topics:
>
> 1. Re: Generating a rising sine tone with sinf() (Adam Bellard)
> 2. Re: Generating a rising sine tone with sinf() (Jim McGowan)
> 3. Re: Generating a rising sine tone with sinf() (Adam Bellard)
> 4. Re: Generating a rising sine tone with sinf() (Jim McGowan)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Thu, 19 Jun 2014 12:55:41 -0400
> From: Adam Bellard <email@hidden>
> To: Jim McGowan <email@hidden>
> Cc: email@hidden
> Subject: Re: Generating a rising sine tone with sinf()
> Message-ID:
> <email@hidden>
> Content-Type: text/plain; charset="utf-8"
>
> Just a wild guess, maybe you are writing interleaved stereo audio out
> instead of mono?
>
> Adam Bellard
>
>
>> On Thu, Jun 19, 2014 at 12:42 PM, 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
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <https://lists.apple.com/mailman/private/coreaudio-api/attachments/20140619/6fa0b8ef/attachment.html>
>
> ------------------------------
>
> Message: 2
> Date: Fri, 20 Jun 2014 01:03:40 +0800
> From: Jim McGowan <email@hidden>
> To: Adam Bellard <email@hidden>
> Cc: "email@hidden" <email@hidden>
> Subject: Re: Generating a rising sine tone with sinf()
> Message-ID: <email@hidden>
> Content-Type: text/plain; charset="gb2312"
>
> Hi Andy
>
> Thanks, but no I'm definitely writing mono.
>
> Jim
>
>> On 20 Jun, 2014, at 0:55, Adam Bellard <email@hidden> wrote:
>>
>> Just a wild guess, maybe you are writing interleaved stereo audio out instead of mono?
>>
>> Adam Bellard
>>
>>
>>> On Thu, Jun 19, 2014 at 12:42 PM, 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
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <https://lists.apple.com/mailman/private/coreaudio-api/attachments/20140620/2e787876/attachment.html>
>
> ------------------------------
>
> Message: 3
> Date: Thu, 19 Jun 2014 13:08:20 -0400
> From: Adam Bellard <email@hidden>
> To: Jim McGowan <email@hidden>
> Cc: "email@hidden" <email@hidden>
> Subject: Re: Generating a rising sine tone with sinf()
> Message-ID:
> <email@hidden>
> Content-Type: text/plain; charset="utf-8"
>
> Like I said, wild guess... Andy looks like he knows what he's talking about!
>
> AB
>
>
>> On Thu, Jun 19, 2014 at 1:03 PM, Jim McGowan <email@hidden> wrote:
>>
>> Hi Andy
>>
>> Thanks, but no I'm definitely writing mono.
>>
>> Jim
>>
>> On 20 Jun, 2014, at 0:55, Adam Bellard <email@hidden> wrote:
>>
>> Just a wild guess, maybe you are writing interleaved stereo audio out
>> instead of mono?
>>
>> Adam Bellard
>>
>>
>>> On Thu, Jun 19, 2014 at 12:42 PM, 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
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <https://lists.apple.com/mailman/private/coreaudio-api/attachments/20140619/4b5d6715/attachment.html>
>
> ------------------------------
>
> Message: 4
> Date: Fri, 20 Jun 2014 01:08:52 +0800
> From: Jim McGowan <email@hidden>
> To: Andreas Tell <email@hidden>
> Cc: "email@hidden" <email@hidden>
> Subject: Re: Generating a rising sine tone with sinf()
> Message-ID: <email@hidden>
> Content-Type: text/plain; charset=gb2312
>
> Hi Andy,
>
> Thanks for that great explanation, I didn't realize that sin(2*pi*f*t) was actually a special case. That helps clear it up.
>
> Thanks,
> Jim
>
>> On 20 Jun, 2014, at 0:51, Andreas Tell <email@hidden> wrote:
>>
>> 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
>
>
>
> ------------------------------
>
> _______________________________________________
> Coreaudio-api mailing list
> email@hidden
> https://lists.apple.com/mailman/listinfo/coreaudio-api
>
> End of Coreaudio-api Digest, Vol 11, Issue 120
> **********************************************
_______________________________________________
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