Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

matplotlib - Increase the resolution of my Python Spectrogram

how can I change the resolution of my spectrogram? I am using the matplotlib.pyplot.specgram function.

I think I would have to enlarge the window but I couldnt find out how to do it. Might be to easy to ask it here stil I hope for an answer.

This is my code:

import numpy as np
import matplotlib.pyplot as plot

#Import data
a1.resize(np.size(a1))
signaldata = a1
samplingFrequency=2000
    
plot.subplot
plot.specgram(signalData,Fs=samplingFrequency)
plot.xlabel('Time in seconds')
plot.ylabel('Frequency')
plot.ylim(0,100)
plot.show()

This is my signal data: a1

This is my spectogram:

enter image description here

In the area of 4-14 seconds and 0-40 (Hz) there are only two bars, with no higher resolution then just two colors. I would hope to see what happens there more specificly. Is there a possibility to increase the resolution?

Thank you in advance.

question from:https://stackoverflow.com/questions/65842503/increase-the-resolution-of-my-python-spectrogram

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The resolution is inherently limited by the Fourier transform, there is not much you can do about it. Still, I believe that the signal you have, has little to no frequency information after 4 seconds. You can try to investigate a bit using tools like the Lomb-Scargle periodogram.

A few years ago I wrote a helper function you may find useful (you can install via pip: pip install fitwrap or download from github).

Here's a snippet you can use, you just need to set the span (window dimension), the minimum and maximum frequency you need min_freq max_freq, the time bins n_bins, and the frequency bins grid_size .

import fitwrap as fw
import matplotlib.pyplot as plt

span = 0.4
n_bins = 100
grid_size = 100
min_freq = 0.01
max_freq = 40

t_tot = 1/samplingFrequency*signaldata.shape[0]
t = np.linspace(0, t_tot, signaldata.shape[0])
tmin = np.min(t)
tmax = np.max(t)
x_bins = np.linspace(tmin+span, tmax-span, n_bins)

spectrogram = np.zeros([grid_size, x_bins.shape[0]])
for index, x_bin in enumerate(x_bins):
    mask = np.logical_and((x_bin-span)<=t, (x_bin+span)>=t) 
    frequency_grid, lombscargle_spectrum = fw.lomb_spectrum(t[mask], signaldata[mask],
                    frequency_span=[min_freq, max_freq], grid_size=grid_size)
    spectrogram[:, index] = lombscargle_spectrum

plt.imshow(spectrogram, aspect='auto', extent=[x_bins[0],x_bins[-1],
            frequency_grid[0],frequency_grid[-1]], origin='lower') 
plot.xlabel('Time in seconds')
plot.ylabel('Frequency')

Lomb


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...