1
$\begingroup$

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:

  1. Tried to plot on colour map with the values it finally returned
  2. 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.

The code:

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[0], samples.strides[0] * 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[0])
    
    # Compute spectrogram feature
    ind = np.where(freqs <= max_freq)[0][-1] + 1
    specgram = np.log(fft[:ind, :] + eps)
    return specgram

MORE DETAILS:

audio_sampling_rate = 48000

UPDATE...

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 is the output...

This audio is a $19$ seconds long. If you need any other graphs specifically please let me know.

$\endgroup$
3
  • $\begingroup$ What makes you think this is wrong? Where exactly are you stuck? Is there something specific that you don’t understand? Do you understand what the output of the spectrogram() function is (essentially what spectroGramVals variable holds)? If you could provide some more information as to what is that you are struggling with we could possibly be able to help a bit more. $\endgroup$
    – ZaellixA
    Sep 22, 2023 at 13:35
  • $\begingroup$ Hello @ZaellixA , i guess the issue i ran up with was in those days, i wasn't able to plot the spectrogram based on the time series... I'm sorry i don't remember exactly, the issue I've faced at this point is i was lacking very much on basics, i was trying to find out why my output is not as same as the output in the guide(was having no clue what i was doing wrong), because the guide's output is based on a time series as far as i remember... $\endgroup$
    – OctoCat
    Sep 24, 2023 at 2:25
  • $\begingroup$ Have you managed to solve the problem? If yes, then could you please provide the solution (you can answer your own questions) and accept so that people with similar (or the same) problems in the future can reference it? $\endgroup$
    – ZaellixA
    Sep 24, 2023 at 10:22

1 Answer 1

2
$\begingroup$

Use librosa.stft() as the reference implementation of a STFT spectrogram. And librosa.display.specshow() the reference fo how to plot a spectrogram.

Then you can compare your implementations to those, to verify that you are on the right track.

$\endgroup$
1
  • $\begingroup$ Hello Jon, thanks a lot for the support, that method is also great, the question is a bit old now, however I'm very much glad for your support! $\endgroup$
    – OctoCat
    Sep 24, 2023 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.