Skip to content

Instantly share code, notes, and snippets.

@ma-laforge
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ma-laforge/5c054cb3e4cc866c9046 to your computer and use it in GitHub Desktop.
Save ma-laforge/5c054cb3e4cc866c9046 to your computer and use it in GitHub Desktop.
dt = 1e-6 #1MHz sampling
f_signal = 5e3 #5khz
SAMPLES_PER_PERIOD = 200
NPERIODS = 150
#Compute sinewave with relative accuracy:
t = range(0, dt, SAMPLES_PER_PERIOD)
sigT = 11.*sinpi(2*f_signal*t)
sig = zeros(SAMPLES_PER_PERIOD*NPERIODS)
for i in 1:NPERIODS
sig[(i-1)*SAMPLES_PER_PERIOD+(1:SAMPLES_PER_PERIOD)] = sigT
end
Fsig = rfft(sig)
#Compute signal-to-noise ratio:
function SNR(Fsig, OSR=64)
p = abs(Fsig[2:end/OSR]).^2 #Normalized pwr - DC excl
S = maximum(p)
N = sum((p .< S).*p)
return 10*log10(S/N)
end
println("\nBase SNR w/64-bit arithmetic:")
@show SNR(Fsig)
:Done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment