# Convolution introduces NaNs

I am performing convolution in MATLAB using the built in conv function. I am convolving an impulse response (length 500 samples) with an 1800 sample long input signal. This input signal contains 450 NaNs at random indexes between sample 530 and 1285. The output signal of this convolution is length 2299, and now consists of 1255 NaNs. I'm unsure why this happens, and how to avoid it?

I'm also not sure I've provided enough info, but if I can add anything else please let me know.

• How did you get the NaN in the input? Was that after processing the raw data? – DSP Novice Apr 3 '20 at 10:51
• Yep! Exactly that – Jack Apr 3 '20 at 10:59
• Its NaN maybe because some number got divided by 0..you can check for that – DSP Novice Apr 3 '20 at 11:00

and how to avoid it?

Address the NaN in your input data. Don't "fix", "paper over it" or "replace by 0". Find the root cause for the NaN, understand what's happening and take meaningful corrective action.

NaN means your input data is bad. Doing anything with bad data is pointless since your output will be bad. Doing cosmetic adjustment just so the code doesn't blow up is equally pointless. You need to understand why the data is bad and address the root cause

I'm unsure why this happens

Well, you know the formula of for convolution. Every single time, no matter what you do, you add to NaN, you get NaN. Same for multiplication.

So, 499 "left and right" of each NaN, you'll get NaN.

how to avoid it?

This might sound stupid, but: don't have NaNs in your input, simple as that!

Whether you want to replace the NaN with 0, or with some other constant, is up to you. Using 0 is usually the wisest approach; that adds no energy to the signal.

You might want to find all places of NaN and replace them with something calculated from the surrounding values, but that's essentially just another interpolating filter. Might or might not be what your application calls for – the fact alone that you've got NaN in your signal indicates you're doing something special, and most likely, the right solution to your problem is fixing what produces these NaN, not trying to work with them.

• Is replacing NaNs with 0s okay though, as that was originally my plan but I was informed it was unwise to do this? The NaNs themselves are intention, I'm working with biomedical data and replace poor data with NaNs for other reasons. – Jack Apr 3 '20 at 10:31
• as said, nobody can tell you! You'd need to describe where the NaN come from / what they mean, and what you're planning to do with the data. To make a bad analogy: "I have holes in my walls. What should I do about that?" can be answered with "apply plaster to holes", but it might also be "wait, you mentioned that you're living near a explosives factory? Maybe these are holes from an explosion, and you need to do a lot of things, before fixing the holes!". But maybe the holes are even there on purpose – maybe your landlord plans to install windows in these holes? – Marcus Müller Apr 3 '20 at 10:41
• Alright, I will go back to discussing this issue. However, for the moment replacing with 0s seems to have worked, thank you very much for the help! – Jack Apr 3 '20 at 10:48