# Averaging 2D signal with ignored samples

I have a 2D signal, and I want to compute a new signal by calculating a weighted mean of each sample's neighbours. However, I also have a 2D bitmask, the same size as the original signal, which indicates that samples in certain positions should be ignored in any average they appear in, including not being counted in the total number of points being averaged, e.g. if my kernel was a 3x3 grid with 1/9 in each entry, then a masked position next to the sample being averaged would mean the sum (of the remaining 8 elements) would be divided by 8 instead of 9. (Unfortunately my kernel is not this simple in practice)

For now, I am calculating this in amplitude space straightforwardly, i.e. for each sample, I go through each value in the kernel, check whether the corresponding cell is masked, and if not, add it to a running total, then divide by the number of unmasked cells.

My understanding is that if I was not masking out samples, this would be a convolution and I could compute the same result more efficiently by taking the FFT of the signal, the FFT of the (zero-padded) kernel, multiplying the two elementwise and then un-FFTing. Being able to do the calculation this way would be a big performance improvement, because I already have to do an FFT of the data for other reasons and so retaining it is "free."

Does anyone know if it is possible to extend the FFT method to take into account that certain samples should be ignored?

• Just a note: the frequency = 0 output of your FFT is absolutely and positively an average, and in a sense so is each frequency bin (they're just averages that have been weighted by sinusoids). If the pixels in question are just generally useless, with invalid data in them, it may be best to replace them with the average of their nearest neighbors first (or implement some other dead-pixel replacement scheme) -- then treat the resulting image as all good, and do your follow-on processing pretending blissful ignorance that you had dead pixels to start with. Commented Dec 14, 2022 at 23:51