I'm following a guide about signal processing, but since I'm a fresher to the domain, the guide just stops at a point where only a function that could return the spectrogram values is written. So, my problem is:
- Since I'm not good on the domain yet, I'm stuck where I don't understand how to plot the spectrogram from the given function.
What I've already tried:
- Tried to plot on colour map with the values it finally returned
- Looked into some guides relevant to spectrograms as well. Didn't help me on what I need to do
What I'm trying to do: I'm actually trying to plot the values that this function returns, but it finally doesn't make any sense since it returns only spectrogram values...
What I would like from the community is: It would be really helpful if you could tell me which areas I need to improve while I'm surfing through the domain of signal processing and any guides that will help me. I'm doing this because I'm on a project where related to an audio processing component.
Also would be glad if you could provide some source code to plot this function's values.
def spectrogram(samples, sample_rate, stride_ms = 10.0, window_ms = 20.0, max_freq = None, eps = 1e-14): stride_size = int(0.001 * sample_rate * stride_ms) window_size = int(0.001 * sample_rate * window_ms) # Extract strided windows truncate_size = (len(samples) - window_size) % stride_size samples = samples[:len(samples) - truncate_size] nshape = (window_size, (len(samples) - window_size) // stride_size + 1) nstrides = (samples.strides, samples.strides * stride_size) windows = np.lib.stride_tricks.as_strided(samples, shape = nshape, strides = nstrides) assert np.all(windows[:, 1] == samples[stride_size:(stride_size + window_size)]) # Window weighting, squared Fast Fourier Transform (fft), scaling weighting = np.hanning(window_size)[:, None] fft = np.fft.rfft(windows * weighting, axis=0) fft = np.absolute(fft) fft = fft**2 scale = np.sum(weighting**2) * sample_rate fft[1:-1, :] *= (2.0 / scale) fft[(0, -1), :] /= scale # Prepare fft frequency list freqs = float(sample_rate) / window_size * np.arange(fft.shape) # Compute spectrogram feature ind = np.where(freqs <= max_freq)[-1] + 1 specgram = np.log(fft[:ind, :] + eps) return specgram
audio_sampling_rate = 48000
spectroGramVals=spectrogram(audio_samples, audio_sampling_rate, max_freq = 48000) fig, axs = plt.subplots() co = axs.contourf(spectroGramVals) fig.colorbar(co) axs.set_title('contourf()') spectroGramVals.shape (481, 1968)
I tried the above code to represent what I wanted to... but still it's wrong... I believe...
This audio is a $19$ seconds long. If you need any other graphs specifically please let me know.