Skip to content

Commit

Permalink
fix(dsp): circcorr was mistakenly called circconv (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
mchitre committed Sep 9, 2024
1 parent a9a968e commit dceb3a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
28 changes: 16 additions & 12 deletions src/dsp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import DSP: DSP, filt, filtfilt, resample, nextfastfft
import Statistics: std
import Peaks: argmaxima, peakproms!
import Optim: optimize, minimizer, BFGS
import FFTW: fft, ifft

export fir, removedc, removedc!, demon
export upconvert, downconvert, rrcosfir, rcosfir
export mseq, gmseq, circconv, goertzel, pll, hadamard
export mseq, gmseq, circconv, circcorr, goertzel, pll, hadamard
export mfilter, findsignal, dzt, idzt
export istft, whiten, filt, filtfilt, resample, delay!, compose

Expand Down Expand Up @@ -329,16 +330,19 @@ $(SIGNATURES)
Computes the circular convolution of `x` and `y`. Both vectors must be the same
length.
"""
function circconv(x::AbstractVector, y::AbstractVector=x)
if length(x) != length(y)
throw(ArgumentError("x and y must be of equal length"))
end
n = length(x)
z = similar(x)
for j 1:n
z[j] = circshift(x, j-1)' * y
end
return z
function circconv(x::AbstractVector, y::AbstractVector)
length(x) == length(y) || throw(ArgumentError("x and y must be of equal length"))
ifft(fft(x) .* fft(y))
end

"""
$(SIGNATURES)
Computes the circular correlation of `x` and `y`. Both vectors must be the same
length.
"""
function circcorr(x::AbstractVector, y::AbstractVector=x)
length(x) == length(y) || throw(ArgumentError("x and y must be of equal length"))
ifft(conj.(fft(x)) .* fft(y))
end

"""
Expand Down Expand Up @@ -739,7 +743,7 @@ function findsignal(r, s, n=1; prominence=0.0, finetune=2, mingap=1, mfo=false)
return (time=Float64[], amplitude=T[], mfo=mfo ? m : empty(m))
end
h = m̄[p]
ndx = partialsortperm(h, 1:n; rev=true)
ndx = partialsortperm(h, 1:min(n,length(h)); rev=true)
p = p[ndx]
if finetune == 0
t = time(Float64.(p), s)
Expand Down
11 changes: 8 additions & 3 deletions test/tests-core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -475,20 +475,25 @@ function test_dsp()
@test y vcat(zeros(22), 1.0, zeros(22)) atol=0.01
@test sum(x.^2) 1.0

x = rand(rng, 256)
@test circcorr(x) circconv(x, conj.(circshift(reverse(x), 1)))

x = rand(rng, ComplexF64, 256)
@test circcorr(x) circconv(x, conj.(circshift(reverse(x), 1)))

for j 2:10
x = mseq(j)
@test x isa Array{Float64}
@test length(x) == 2^j-1
@test all(abs.(x) .== 1)
y = circconv(x)
@test all(y .== circconv(x, x))
y = circcorr(x)
@test y[1] length(x)
@test all(y[2:end] .≈ -1)
x = gmseq(j)
@test x isa Array{ComplexF64}
@test length(x) == 2^j-1
@test all(abs.(x) .== 1)
y = circconv(x)
y = circcorr(x)
@test y[1] length(x)
@test rms(y[2:end]) 0 atol=1e-10
end
Expand Down

0 comments on commit dceb3a2

Please sign in to comment.