psambit9791 / jdsp

A Java Library for Digital Signal Processing
https://jdsp.dev
MIT License
240 stars 45 forks source link

Feature Request: Zero-padding for DFT #24

Closed SiboVG closed 2 years ago

SiboVG commented 2 years ago

It would be a fairly simple implementation, since a zero padding method already exists in the utils methods, but having zero padding implemented in the dtf-method of DiscreteFourier would be handy.

psambit9791 commented 2 years ago

Zero-padding itself is not considered a part of DFT. In most implementations, the signal is padded separately and then the DFT is called. Implementation here will look something like this:

N = 20; //Such that padding length is less than signal length
double[] paddedSignal = UtilMethods.padSignal(signal, N);

DiscreteFourier df = new DiscreteFourier(paddedSignal);
df.ffit();
SiboVG commented 2 years ago

Well, in the MATLAB-implementation of fft, you do have the possibility of the FFT-length, which will either truncate the signal or zero-pad it. Since zero-padding is very often used, maybe it makes sense to implement dft-size inputting, so that users don't have to write the zero-padding code.

image

psambit9791 commented 2 years ago

This could lead to a lot of additional complexities in the DiscreteFourier class. One of the prime things that surfaces is the mode of truncation and padding. The existing padding pads on either sides in various modes, however in MATLAB, the padding is only on the end with zeros. Then comes the question of truncation. How will the truncation be implemented; will it be truncating from the middle as in: signal [n/2:-n/2]; or just chop off the end or the start?

I think the present implementation is simple and allows for signal modification as required in an extra line of code mostly. The prime reason to avoid this would be to keep the documentation simple and the APIs easy to use.