i am having a project for university to translate a matlab program into python languague. It’s a script regarding Cerebro-acoustic coherence.
For the matlab program i printed the results step by step to ensure i replicate the results.
So far the python script is identical until to the part of spectrogram, where suddenly the results get bonkers.
In the original code of matlab i have code lines using spectrogram like this:
fs = 250
window = hann(500);
[x1, f1, t1] = spectrogram(data(501:end)',window,[], [0:0.25:125], fs,'psd');
Reading through lots of stuff, it seems that both, the scipy.signal.spectrogram in Python and the spectrogram from Matlab should be identical but i dont get out the same results. So i wondered if there are some default parameters that work differently or anything else, that distorts my results.
Do you have an idea?
The Results are extremly off.
After Spectrogram i get a vector with imaginary and real numbers.
e.g. first 5 values using fprinf %s
[34.7119804789025-1.94289029309402e-16i -1.40769181257669-
30.6097088273847i -20.8637730426081+1.27750647086169i -0.248935482997265+11.
0511268275279i 5.47494085665919+1.6172651442504i]
For spectrogram in Python i dont get these or the numbers are just really off.
Do you have an idea?
In Python i tried this.
window_length = 500
noverlap = window_length // 2 # MATLAB default is half the window length
nfft = 1000 # 1000 because fs/step_size = 250/0.25 = 1000
# Create Hanning window
nperseg = 500
freq_range = np.arange(0, 126, 0.25) # Frequency range in Hz, similar to MATLAB's [0:0.25:125]
#define 500 samples hanning window
f1, t1, x1 = scipy.signal.spectrogram(All_sound_cochl_enter_time[500:], detrend=False, fs=250, window='hann', nperseg=window_length, noverlap=overlap, axis=0, nfft=None, scaling='density', mode='psd')
Jessica Val is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.