Skip to content

Commit

Permalink
refactor(dsp): add fc to pll()
Browse files Browse the repository at this point in the history
  • Loading branch information
mchitre committed Jul 7, 2024
1 parent 22b9c72 commit 61082d5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/dsp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,19 @@ end

"""
$(SIGNATURES)
Phased-lock loop to track dominant carrier frequency in input signal.
Phased-lock loop to track carrier frequency (approximately `fc`) in the input signal.
If `fc` is not specified, the algorithm will attempt to track the dominant frequency.
"""
function pll(x::AbstractVecOrMat, bandwidth=1e-3; fs=framerate(x))
function pll(x::AbstractVecOrMat, fc=0.0, bandwidth=1e-3; fs=framerate(x))
fs = inHz(fs)
fc = inHz(fc)
β = bandwidth
n = nchannels(x)
ϕ = zeros(1,n)
ω = zeros(1,n)
ϕ = zeros(1, n) # phase estimate
ω = zeros(1, n) # integrator
y = similar(x, ComplexF64)
for j 1:nframes(x)
y[j,:] = cis.(ϕ)
y[j,:] = cis.(-2π * fc * (j-1)/fs .+ ϕ)
Δϕ = angle.(x[j,:] .* conj.(y[j,:])) .* abs.(x[j,:])
ω .+= bandwidth * Δϕ
ϕ .+= β*Δϕ .+ ω
Expand Down
2 changes: 1 addition & 1 deletion test/tests-core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ function test_dsp()
x = cw(1kHz, 1s, 80kHz)
x2 = x .+ 0.25*rand(rng, size(x)...) .+ 0.25im*rand(rng, size(x)...)
e2 = mean(abs2.(x[800:end] .- x2[800:end]))
x3 = pll(x2)
x3 = pll(x2, 1kHz)
@test meanfrequency(x3[800:end]) meanfrequency(x[800:end]) atol=0.1
e3 = mean(abs2.(x[800:end] .- x3[800:end]))
@test e2/e3 > 3
Expand Down

0 comments on commit 61082d5

Please sign in to comment.