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

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?

``````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

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

``````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

3 Likes

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

1 Like

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.

1 Like

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.

``````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

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

5 Likes

it was a sample rate issue as you said.

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

1 Like

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 “.”.

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.

3 Likes

`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`).

1 Like