juliasignalsnyquist

sampling a signal above nyquist frequency in julia


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


Solution

  • 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.