Convolution introduces NaNs
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