Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

circular convolution implementation #44

Closed
a2ray opened this issue Sep 3, 2024 · 2 comments · Fixed by #45
Closed

circular convolution implementation #44

a2ray opened this issue Sep 3, 2024 · 2 comments · Fixed by #45
Assignees
Labels
bug Something isn't working

Comments

@a2ray
Copy link

a2ray commented Sep 3, 2024

Hi folks,

In the implementation of circular shift here:

function circconv(x::AbstractVector, y::AbstractVector=x)

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
end

I believe it should be instead

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)
  xuse = circshift(reverse(x), 1) # CHANGED
  for j  1:n
    z[j] = circshift(xuse, j-1)' * y # CHANGED
  end
  return z
end

I made the same mistake when trying to figure out why a convolution behaved the way it did (instead of doing circular convolution using FFT multiplication)

@mchitre mchitre self-assigned this Sep 6, 2024
@mchitre mchitre added the bug Something isn't working label Sep 6, 2024
@mchitre
Copy link
Member

mchitre commented Sep 6, 2024

#45 fixes this. @a2ray would you like to take a quick look the verify before I merge?

@a2ray
Copy link
Author

a2ray commented Sep 9, 2024

circconv looks good to me and circcorr follows the definition. I suppose for circcorr to be linear as opposed to circular it also needs both sequences of length n and m to be zero padded to n+m-1? Lastly, another test could be added interchanging input sequences to ensure that the result is commutative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants