Last active
August 29, 2015 14:25
-
-
Save ma-laforge/5c054cb3e4cc866c9046 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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