# Why the following code is not generating the normal wave period?

**URL:** <https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341>\
**Category:** New to Julia\
**Tags:** question, plots, dsp\
**Created:** [September 17, 2021, 3:17pm UTC](https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341 "2021-09-17T15:17:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [September 17, 2021, 3:17pm UTC](https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341/1 "2021-09-17T15:17:07Z")

</div>

Hello. Good afternoon

I’m trying to do signal process with julia, and I’m new in language. I m having trouble with language to generate a simple signal(i came from matlab and octave). May someone help?

```julia
using Plots

frequency = 3
x = range(0, stop = 100, step = 1)
y = sin.(2π * x * frequency)
plot(x, y, lw = 3)

```

The above code is generating a wrong signal  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/f/8fdcd794ab7f67874a3b1ce7ecdca6b07674becb.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 17, 2021, 3:22pm UTC](https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341/2 "2021-09-17T15:22:08Z")

</div>

Your sampling rate is too coarse (every 1 s) for a signal frequency of 3 Hz.  
Try:

```julia
x = range(0, stop = 1, step = 0.01)

```

or alternatively, reduce further the frequency to 0.03 Hz for example, and do not touch the rest of your code.

_NB: basically you do not have enough samples per period for a nice sine wave plot display, that is all. The time/frequency units were just an example_

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [September 17, 2021, 3:22pm UTC](https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341/3 "2021-09-17T15:22:20Z")

</div>

Are you use you don’t want `y = sin.(2π * x / frequency)`?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [September 17, 2021, 3:23pm UTC](https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341/4 "2021-09-17T15:23:16Z")

</div>

You’re computing sin at multiples of 6 pi, and they are all essentially zero. Maybe you want `sin.(2π * x / frequency)` or even more samples per cycle.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [September 17, 2021, 3:33pm UTC](https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341/5 "2021-09-17T15:33:27Z")

</div>

Hello. The sample rate was wrong. I was mususing the range function, and got confused with another matlab function linspace, that you pass the number of points in signal resolution. This range works as counter part of linspace.

```julia
using Plots

frequency = 3
sample_rate = 100
x = range(0, stop = 1, step = 1/sample_rate)
y = sin.(2π * x * frequency)
plot(x, y, lw = 3)

```

Thank you all by the explanation

![image](https://global.discourse-cdn.com/julialang/original/3X/9/a/9a91a338a1bafa0654d45306239d295b9e3bb056.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 17, 2021, 3:35pm UTC](https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341/6 "2021-09-17T15:35:57Z")

</div>

@affans, @Jeff_Emanuel, we should not divide by the frequency, that is the role of the wave period. 😉

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [September 17, 2021, 3:39pm UTC](https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341/7 "2021-09-17T15:39:48Z")

</div>

it was a sample rate issue as you said.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 17, 2021, 3:44pm UTC](https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341/8 "2021-09-17T15:44:16Z")

</div>

@jcbritobr, maybe `x` in original code was more like this in Julia:  
`x = LinRange(0, 1, 100)` ?

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [September 17, 2021, 3:46pm UTC](https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341/9 "2021-09-17T15:46:09Z")

</div>

Im very new to julia. Did not know about that. Seems is similar to linspace. I had a trouble even with sin function. I need to search a lot to understand that sin just dont vectorize without the dot “.”.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [September 17, 2021, 3:55pm UTC](https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341/10 "2021-09-17T15:55:26Z")

</div>

But, I’m enjoining a lot plot data in julia. Its way faster than python, scilab or octave. The svg aproach permits good performance with high resolution signals. I can zoom between withou any lag.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/b/4beeb3461258e0d5f6fde6f42fce8eefde0babed.png)

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [September 18, 2021, 11:02pm UTC](https://discourse.julialang.org/t/why-the-following-code-is-not-generating-the-normal-wave-period/68341/11 "2021-09-18T23:02:06Z")

</div>

> [@jcbritobr](#):
>
> Seems is similar to linspace.

`range(0, 1, 100)` is the same as MATLAB’s `linspace(0, 1, 100)` but is way more efficient (doesn’t allocate a vector of 100 elements) and more accurate (`TwicePrecision`).
