To create a spectrogram from an array using matplotlib's specgram function with a 0.9 overlap and a 0.4 seconds Kaiser window sampled at 50 Hz, you can use the following code:
code
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import get_window
# Generate some example data
data = np.random.randn(5000) # 100 seconds of data sampled at 50 Hz
# Create a Kaiser window with a length of 0.4 seconds (20 samples)
window_length = int(0.4 * 50)
window = get_window('kaiser', window_length, beta=5)
# Create the spectrogram using matplotlib's specgram function
f, t, Sxx = plt.specgram(data, NFFT=window_length, Fs=50, window=window, noverlap=int(0.9 * window_length))
# Plot the spectrogram
plt.pcolormesh(t, f, Sxx, shading='gouraud')
plt.colorbar()
plt.title('Spectrogram')
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()
This code first generates some example data, then creates a Kaiser window with a length of 0.4 seconds (20 samples, since the sampling frequency is 50 Hz). It then uses matplotlib's specgram function to create the spectrogram, with the following arguments:
data: the array of data to be used to create the spectrogram
NFFT: the length of the Fourier transform used to compute the spectrogram. In this case, it is set to the length of the Kaiser window.
Fs: the sampling frequency of the data.
window: the window function used to divide the data into segments.
noverlap: the number of samples that each segment of the data overlaps with the previous segment. In this case, it is set to 0.9 times the length of the Kaiser window.
Finally, the code uses matplotlib's pcolormesh function to plot the spectrogram, and adds a colorbar, title, and axis labels.
I hope this helps! Let me know if you have any questions.
Note: The beta parameter in the get_window function controls the shape of the Kaiser window. Higher values of beta will result in a window that is more tapered, and will therefore have a better frequency response but a worse time response. Conversely, lower values of beta will result in a window that is less tapered, and will therefore have a worse frequency response but a better time response. In this example, I used a value of beta=5, but you can experiment with different values to see how they affect the spectrogram.
Note 2: The nperseg parameter in the specgram function can also be used to control the length of the Fourier transform used to compute the spectrogram. If you set nperseg to a value that is different from the length of the window, then the window will be zero-padded or truncated to match the length of nperseg. For example, if you set nperseg=100 and window_length=20, then the window will be zero-padded to a length of 100 before the Fourier transform is computed. Conversely, if you set nperseg=10 and window_length=20, then the window will be truncated to a length of 10 before the Fourier transform is computed.
In general, it is best to use a value of nperseg that is equal to the length of the window, in order to avoid zero-padding or truncation. However, you can experiment with different values of nperseg to see how they affect the spectrogram.