c++calgorithmmathwaveform

Determining total time for square wave frequency to increase from f1 to f2, with a linear rate of change


I'm trying to determine the total length of time that a square wave takes to linearly increase from frequency 1 to frequency 2.

Question 1: If I start at, for example: f1=0hz f2=1000hz step increase per cycle is 10hz What is the total time that elapses for this linear increase of frequency to take place?

Question 2: If I have: f1=0hz f2=1000hz and I want the increase to take place over 5 seconds, for example, how would I calculate the rate of interval increase per cycle to achieve this. *(basically the inverse of question 1)

This is for a hobby project to make a faster stepper motor driver profile, for programming an Atmel microcontroller (in C [well, Atmel's Arduino "C"]).

Any thoughts would be helpful. Thank you in advance!

I found this sine wave that slowly ramps up frequency from f1 to f2 for a given time but this answers a slightly different question - and is for a Sine wave.


Solution

  • @T Brunelle,

    Q1. Your time for a general frequency would be

    t=(1/(delta f))*ln(f2/f1)

    where f1 and f2 are start and end frequencies and (delta f) is the change per cycle. Obviously this won't work if f1 is 0, but then a zero-frequency oscillation doesn't make sense either.

    This comes from solving (in practice, just multiplying together and using the chain rule):

    df/d(phase)=(delta f)

    and

    d(phase)/dt=f

    where "phase" increases by 1 each cycle.

    Q2. Just invert the formula in Q1.

    This assumes that the change is linear PER CYCLE (rather than PER TIME, as was in your linked page). In your case it is also independent of the waveform shape.