2

I generate pulses using microcontroller with following method:

  1. Set $A=0$
  2. With frequency $F$ execute the following code:

    2.1. Add $S$ to $A$

    2.2. If $A\ge N$ then produce a pulse and substract $N$ from $A$

Here $A, S, N\in \mathbb N$, and $S\le N$.

It's easy to see that period between the pulses may be not constant, but let's call the frequency $f$ of the pulses the limit of number of pulses per time $T$ divided to $T$ when $T\to \infty$.

How to compute $f$? I can compute it for concrete numeric examples, but I need general symbolic solution.

Canis Lupus
  • 2,565
  • Can you give an example of how you compute it with a concrete numeric example? Also, the way you wrote it up, as soon as $A\geq N$ the first time, the next time it will be less than $N$, the next time it will be greater than $N$ and repeated. Because it subtracts and adds the same thing to $A$ each time. Is that how it's supposed to be? – BeaumontTaz Jul 11 '14 at 10:19
  • 1
    Sorry, I had a mistake. Substract N, not S. And S<N. Example: N=1000, S=100, then f=F/10 (exact). Of course, most interesting is when S isn't a divisor of N. – Canis Lupus Jul 11 '14 at 10:30
  • 1
    Just multiply by $F$. It will become $f=\frac{FS}{N}$. Also note that for $S\geq N$, $f=F$. – BeaumontTaz Jul 11 '14 at 10:36

1 Answers1

1

As mentioned in the comments, at $T\to\infty, f=\frac{FS}{N}$ for $N>S$ and for $N\leq S$ your code will start double pulsing periodically.

However, that's the the limit. If you aren't looking at long term behavior, this is what you'll follow:

Define $r\equiv N\pmod{S}$. (i.e. $r$ is the remainder after dividing $N$ by $S$.) The number generation will be cyclic over $S$ pulses. So every $S$ pulses, you have the same pattern again. However, in this cycle, some of the pulses will be $F$ longer than the rest. Denote $P=\lfloor \frac{N}{S}\rfloor$. You will have $S-r$ pulses of length $\frac{P}{F}$ and $r$ pulses of length $\frac{P+1}{F}$. However, the longer pulses will be evenly spaced out in the cycle.

For example: take $N=30$ and $S=7$. We know $r=2$ and $P=4$.

Here is the cycle:

$$7,14,21,28,35$$ $$12,19,26,33$$ $$10,17,24,31$$ $$8,15,22,29,36$$ $$13,20,27,34$$ $$11,18,25,32$$ $$9,16,23,30$$ $$\vdots$$

You have a cycle of length $7$ where $7-2=5$ of them are of length $P$ and $r=2$ of them of length $P+1$.

However, for fast enough $F$, it will be hard to notice the difference between this "varying" frequency pulse pattern and a pulse sequence of frequency $\frac{FS}{N}$.

BeaumontTaz
  • 2,795
  • There is more that we can talk about based on the greatest common divisor of N and S and how that affects r and the cyclic pattern that I'll write stuff up on if you'd like. – BeaumontTaz Jul 11 '14 at 11:21
  • And also stuff to talk about with $S>N$. – BeaumontTaz Jul 11 '14 at 11:42
  • Thanks. I know about varying, but for my case it isn't critical, because fluctuations of pulse's appearing time is $\le 1/F$ (compared with exact frequency) , if I correctly understand this. Typical values: $N=10^9, F=1000$ Hz, $f$ is several hundreds Hz. This algorithm is based on DDS. – Canis Lupus Jul 11 '14 at 12:09
  • Yeah. You will be fine with just assuming $f=\frac{FS}{N}$. Best of luck! – BeaumontTaz Jul 11 '14 at 17:41