Unfold and fold to apply the overlap and add algorithm

Hi, I have an audio processing problem, first I need to chunk and put through and rnn, i start with a tensor of shape
(64, 16308) then I apply fold:

tensor.fold(-1, 178, 89)

so I get the tensor with shape (64, 182, 178) with nice overlapping sections

now I can pass it through my rnns. Problem is, now I would like to apply the overlap and add method to get the original tensor back, I thought nn.Fold would be a good match to try and do that, but I dont understand its use well enough to know if I can even use it to do that.

I assume you were using tensor.unfold to create the patches.
If that’s the case, nn.Fold should be working as it’s the reverting operation to nn.Unfold.
Where are you currently stuck and what have you tried so far?

Last night, after a lot of try and error I got nn.Unfold to work, what I had to do was now that I had a tensor with shape (64,182, 178) I transposed it to (64, 178, 182) and now i can use nn.Fold(my_tensor, output_size=(1 ,16198), kernel_size=(1, 178) stride=(1, 89)) to get back a tensor with the original size of (1, 16198) with the overlapping sections added, completing the overlap and add algirithm, sorry that i said my original tensor was shaped (1, 16308) when it is actually (1, 16198). Still not very sure of how nn.Fold works though.

1 Like

I am using nn.fold to apply overlap-add on a sequence of frames generated by a GRU layer. However, the direct addition of the overlapped frames without weighting leads to unstable training behavior.

Is there anyway I can have weighted overlap-add algorithm through nn.fold? Or it least to enable nn.fold with the possibility of taking the mean of the overlapped frames as done in multi-channel convolutional layers?

Hey @Dalisson_Figueiredo, for 1D signals, I’d avoid using nn.Fold and nn.Unfold, as possible, since they mostly are for some 2D function, I guess.
torch.Tensor.unfold does exactly what is expected in this case!

I wonder, if they will add torch.Tensor.fold or it might already exist!?!