JuliaDSP / DSP.jl

Filter design, periodograms, window functions, and other digital signal processing functionality
https://docs.juliadsp.org/stable/contents/
Other
379 stars 109 forks source link

Resample doesn't resample at given rate #186

Open dsweber2 opened 6 years ago

dsweber2 commented 6 years ago

For example, simply trying to double the length of a signal doesn't actually work:

x = randn(4)
resample(x,4.0)
resample(x,4//1)

both of which return a vector of length 14. I tried various other lengths for x and got that it down by 2 for every case.

I tried altering the amount of zero padding, but increasing the padding by even 1 led to a length 18 signal. I suspect that something is wrong with the actual resample_filter design, but I'm not knowledgeable enough with the material to fix it on my own at this point.

And while we're at it, not being able to put in an integer as a resampling rate is mildly annoying. Is there any design reason for this?

dsweber2 commented 6 years ago

Another issue-- both this and the matlab version assume that the signal is periodic (or at least continuous at the boundaries), leading to some pretty nasty gibbs phenomina (e.g. plot([x for x=0:.1:10])). Since this is pretty easy to fix by mirroring the signal (using resample([x; flipdim(x,1)],2.0)), I'm surprised that neither matlab nor this DSP code does this. If there's a speed concern, checking the difference between the first and last entry relative to the mass of the whole signal (e.g. (x1]-x[end])/norm(x)>ϵ should be a sufficient check (if their neighbors are too far from the current, simply mirroring isn't going to cut it).

staticfloat commented 6 years ago

I don't think that automatically mirroring the signal is a good idea, as I would want to opt into that if I wanted to actually do that.

However, I agree that allowing the multirate filters to eat some samples isn't good; if I ask for a 2//1 resampling factor, I should get exactly 2x as many samples out. For example, I would expect resample(randn(4), 2//1) to give me the number of output samples as is declared by DSP.outputlength(FIRFilter(h, 2//1), length(x)).