2

I essentially have three data points in three separate numpy array and I want to plot the time along the x-axis, the frequency along the y-axis and the magnitude should represent the colors. But this is all happening in real time so it should look like the spectrogram is drawing itself as the data updates.

I have simplified the code to show that I am running a loop that takes at most 10 ms to run and during that time I am getting new data from functions.

import numpy as np
import random
from time import sleep

def new_val_freq():
   return random.randint(0,22)

def new_val_mag():
   return random.randint(100,220)

x_axis_time = np.array([1])
y_axis_frequency = np.array([10])
z_axis_magnitude = np.array([100])

t = 1
while True:
   x_axis_time = np.append (x_axis_time , [t+1])
   t+=1

   y_axis_frequency = np.append (y_axis_frequency , [new_val_freq()])
   z_axis_magnitude = np.append (z_axis_magnitude, [new_val_mag()])
   
   time.sleep(0.01)
   #Trying to figure out how Create/Update spectrogram plot with above additional 
   #data in real time without lag

Ideally I would like this to be as fast as it possibly can rather than having to redraw the whole spectrogram. It seems matplolib is not good for plotting dynamic spectrograms and I have not come across any dynamic spectrogram examples so I was wondering how I could do this?

lionheart
  • 333
  • 2
  • 11
  • Can you fill out the create spectrogram section with the code you are using? – Ianhi Sep 09 '20 at 21:38
  • In short, if you are using imshow then you should use the `set_data` method. This may be also possible when using pcolormesh but I think it's more difficult – Ianhi Sep 09 '20 at 21:38
  • for animating pcolormesh see https://stackoverflow.com/questions/18797175/animation-with-pcolormesh-routine-in-matplotlib-how-do-i-initialize-the-data – Ianhi Sep 09 '20 at 21:40
  • That is what I have been trying to figure out. How to do the spectrogram plotting in real time without any lag. I used the matplotlib animation concept found from this [github](https://github.com/ayared/Live-Specgram) which draws the spectrogram of the sound obtained from your mic. But that is also slow no matter how much I reduce the ```interval``` parameter. – lionheart Sep 09 '20 at 21:41
  • even just the code you would use to create a static spectrogram would be helpful – Ianhi Sep 09 '20 at 21:41
  • Also I see that part of the issue is that you want to check for external input. I think you may end up finding that this best solved using threads or async for input. You may also want to check out bokeh and streaming data. e.g. http://holoviews.org/user_guide/Streaming_Data.html – Ianhi Sep 09 '20 at 21:48
  • I agree with @Ianhi that any "work" to get the data should probably be in another thread or something similar. Since you tagged this vispy, you could try the [SpectrogramVisual](https://github.com/vispy/vispy/blob/33b99126611c45c3bccd003c05cd7e7547ddf478/vispy/visuals/spectrogram.py) and do `my_spectrogram_visual.x = new_data`. However, for the coloring you want, you may have to customize the `_calculate_spectrogram` method. Pull requests are welcome if you end up going this route. Feel free to make an issue for further discussion. – djhoese Sep 30 '20 at 14:03

0 Answers0