I'm trying to learn about why signals that cross in an STFT cause apparent artifacts in the magnitude. I've been looking at the example below, written in python, where a gaussian peak overlaps with a sinusoidal wave.
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
time = time = np.arange(0,1,0.01)
sine = np.cos(2*np.pi*10*time)
gaus = 4.0 * signal.windows.gaussian(time.shape[0],2)
plt.plot(time, sine + gaus, '.-')
plt.xlabel("Time (s)")
plt.ylabel("Signal")
plt.show()
When I take the STFT of the signal and look at the magnitude of the output, there are some artifacts or edge effects near where the gaussian and sinusoidal signals overlap in time-frequency space. They appear as slight decreases in the magnitude.
fs = 100
window='hamming'
nperseg=20
noverlap=18
nfft = 2*nperseg
f,t,Zxx_sum = signal.stft(sine + gaus, fs=fs, window=window, nperseg=nperseg, \
noverlap=noverlap, nfft=nfft, return_onesided=True)
plt.pcolormesh(t,f,np.abs(Zxx_sum),cmap='magma')
plt.colorbar(label="STFT magnitude")
plt.xlabel("Time (s)")
plt.ylabel("Frequency (Hz)")
plt.show()
What is the origin of the slight decreases in the magnitude? Is this related to the window or the overlap? Why does the STFT magnitude of this signal not equal the sum of the magnitudes of the individual parts? I have plotted this below for clarity.
f,t,Zxx_sine = signal.stft(sine, fs=fs, window=window, nperseg=nperseg, \
noverlap=noverlap, nfft=nfft, return_onesided=True)
f,t,Zxx_gaus = signal.stft(gaus, fs=fs, window=window, nperseg=nperseg, \
noverlap=noverlap, nfft=nfft, return_onesided=True)
plt.pcolormesh(t,f,(np.abs(Zxx_sine) + np.abs(Zxx_gaus)) - np.abs(Zxx_sum),cmap='magma')
plt.colorbar(label="STFT magnitude")
plt.xlabel("Time (s)")
plt.ylabel("Frequency (Hz)")
plt.show()