• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Generating a rising sine tone with sinf()
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Generating a rising sine tone with sinf()


  • Subject: Re: Generating a rising sine tone with sinf()
  • From: Jim McGowan <email@hidden>
  • Date: Fri, 20 Jun 2014 01:08:52 +0800

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
>

 _______________________________________________
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


References: 
 >Generating a rising sine tone with sinf() (From: Jim McGowan <email@hidden>)
 >Re: Generating a rising sine tone with sinf() (From: Andreas Tell <email@hidden>)

  • Prev by Date: Re: Generating a rising sine tone with sinf()
  • Next by Date: Generating a rising sine tone with sinf() (Adam Bellard)
  • Previous by thread: Re: Generating a rising sine tone with sinf()
  • Next by thread: Re: Generating a rising sine tone with sinf()
  • Index(es):
    • Date
    • Thread