0

I am trying to build a signal containing the frequencies 10 Hz and 1 Hz with amplitudes 10 and 1 respectively and simulate this signal by sampling above Nyquist frequency in Julia

and this is my code

using Plots

T = 10
delta = 0.01
ts = 0:delta:T
omega =0.5*pi/delta
f = sin.(omega*ts)+10*sin.(10*omega*ts)
plot(f)

but the plot I am getting is no where close to real output , Please help me find my error

loop
  • 7
  • 3
  • Your `omega` value is very wrong, and your time vector is absurdly long, 100,000 points. – DNF Aug 12 '22 at 19:43
  • @DNF can you please explain why my omega value is wrong , from the Nyquist therom , omega should be less than pi/delta so I picked pi/2delta as the value – loop Aug 12 '22 at 19:51
  • It's good that you reduced `T` to 10, that makes the plot easier to read. But Nyquist doesn't say `omega` should be less than `pi/delta`, it says that the _frequency_ should be. And the frequency for the second signal is `10*omega`. You make it much easier for yourself if you write `sin.(2pi.*f1.*t) .+ 10 .* sin(2pi.*f2.*t);` set `f1 = 1`, `f2=10`, and then select the sampling rate accordingly. – DNF Aug 12 '22 at 20:35
  • @DNF thanks for the suggestion , can you wirte the the answer elaboratly please – loop Aug 13 '22 at 03:01

1 Answers1

0

As DNF pointed out, you need to understand what the parameters of the sine wave discussed in the Nyquist theorem are. TO do so, you should understand how to plot a sine wave in a way that you can actually see as a wave on your plot. That usually means not having hundreds or thousands of cycles crammed into a few centimeters on your screen. So you should go back to your first sine plot and investigate how changing the frequency, duration, amplitude, and sample rate affect the plot's appearance. Does your sine plot make sense with your parameters?

Consider (sinpi(x) is just sin(pi * x)):

function sinewave(frequency, duration, amplitude, samplerate)
    x_vector = collect(0 : 1 / samplerate : duration * samplerate)
    y_vector = map(sinpi, 2 * frequency .* x_vector ./ samplerate)
    return x_vector, y_vector .* amplitude
end

If we choose a frequency of 10 Hz and a duration of 10 seconds, there will be 100 peaks at the top of our graph. That is too many for me to see the curves well, on my screen at least. So pick 3 seconds.

Nyquist says the minimum sampling rate needs to be at least twice the highest frequency we want to detect. Otherwise we may miss a cycle between our samples.

nyquist_rate(freqs) = maximum(freqs) * 2

myrates = [1.0, 10.0]
fs = nyquist_rate(myrates) # 20

x1, y1 = sinewave(1.0, 3.0, 1.0, fs)
x10, y10 = sinewave(10.0, 3.0, 10.0, fs)

plot(x1 .+ x10, y1 .+ y10)

Now you should be able to see the plot.

Bill
  • 5,600
  • 15
  • 27