<p>There is in fact a way to avoid this effect and it is outlined by the NYQUIST sampling theorem - which states that the sampling rate should be at least twice as high as the maximum frequency of the function of interest. <br>
In this case where <code>cos(a * t)</code> is the function of interest, the frequency is <code>a/(2*pi)</code> - therefore your sampling rate should be greater than <code>2 * a/(2*pi)</code> (in this case you may even want the sampling rate to be even higher so that the cosine function has a nice curve to it). <br>
To get the step size from this sampling rate just use <code>t = 1/(sampling rate)</code> <br>
Hope this helps :)</p>

CC BY-SA 4.0
2020-12-27T09:08:55.133Z
thank you! made my day! i knew of this in audio processing, but couldnt build the cnnection! you are a hero!
Sean
CC BY-SA 4.0
2020-12-27T06:54:58.417Z
2020-12-27T06:54:58.417Z
Elijah Nicol
<p>I am working with the cosine function with a very large input, like <code>cos(a* t)</code>, where <code>t</code> varies between  <code>1</code> and <code>~ 200</code>; and <code>a</code> can be as large as <code>10^8</code>-</p>
<p>I come across, as expected, the artifact error. If I sample <code>t</code> with a stepsize of <code>0.01</code> with <code>a</code> 100, I get a plot like this :</p>
<p><a href="https://i.sstatic.net/H496n.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/H496n.png" alt="enter image description here" /></a></p>
<p>Notice, that for large value of <code>a</code>, the sampling of <code>t</code> needs to be fine, otherwise, you will not get regular cosine shape from <code>-1</code> to <code>1</code>. Sometimes, wave peaks, and other times wave troughs will be missed, due to low granularity, and you will end up with a different shape of the wave than the theoritical value.</p>
<p>Now, increase <code>a</code> to be <code>100000</code>, and as expected, the effect is much more pronounced.</p>
<p><a href="https://i.sstatic.net/YjEcf.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/YjEcf.png" alt="enter image description here" /></a></p>
<p>My question : Is there a formula / algorithm, to avoid this effect? Is there a formula, that tells you the correct step size for a large value of <code>a</code> ?</p>
<p>I tried to google the following keywords : <code>large input cosine function computer graphics</code>, but that did not help.</p>
<p>Thank you.</p>

<sampling><2d-graphics>
Algorithm for proper drawing of cosine function
Sean
# Algorithm for proper drawing of cosine function

(Asked by: username_0 on 2020-12-27)

I am working with the cosine function with a very large input, like `cos(a* t)`, where `t` varies between `1` and `~ 200`; and `a` can be as large as `10^8`-

I come across, as expected, the artifact error. If I sample `t` with a stepsize of `0.01` with `a` 100, I get a plot like this :

[![enter image description here](https://i.sstatic.net/H496n.png)](https://i.sstatic.net/H496n.png)

Notice, that for large value of `a`, the sampling of `t` needs to be fine, otherwise, you will not get regular cosine shape from `-1` to `1`. Sometimes, wave peaks, and other times wave troughs will be missed, due to low granularity, and you will end up with a different shape of the wave than the theoritical value.

Now, increase `a` to be `100000`, and as expected, the effect is much more pronounced.

[![enter image description here](https://i.sstatic.net/YjEcf.png)](https://i.sstatic.net/YjEcf.png)

My question : Is there a formula / algorithm, to avoid this effect? Is there a formula, that tells you the correct step size for a large value of `a` ?

I tried to google the following keywords : `large input cosine function computer graphics`, but that did not help.

Thank you.

**Tags:** sampling, 2d-graphics


---

## 1 answers

### Answer by username_1 on 2020-12-27. Score: 1 | Accepted answer

There is in fact a way to avoid this effect and it is outlined by the NYQUIST sampling theorem - which states that the sampling rate should be at least twice as high as the maximum frequency of the function of interest.   
In this case where `cos(a * t)` is the function of interest, the frequency is `a/(2*pi)` - therefore your sampling rate should be greater than `2 * a/(2*pi)` (in this case you may even want the sampling rate to be even higher so that the cosine function has a nice curve to it).   
To get the step size from this sampling rate just use `t = 1/(sampling rate)`   
Hope this helps :)

> **Comments:**
> **username_0**: thank you! made my day! i knew of this in audio processing, but couldnt build the cnnection! you are a hero!  

---

