Re-creating NeuroExplorer's perievent spectrogram with MATLAB

30 views (last 30 days)
Andy Garcia Barrabeitg
Andy Garcia Barrabeitg on 22 Oct 2021
Commented: Dave B on 11 Nov 2021 at 13:28
I am trying to create a perievent spectrogram aka time-frequency spectrogram from a matrix of voltage values where each row is a trial and each column is a different time point (sampling frequency 1000). How could I go about transforming this matrix into power values (in decibels) where each row is a different frequency value and each column represents a different time point?

Answers (1)

Dave B
Dave B on 22 Oct 2021
The function you're looking for here is spectrogram, but getting it to produce an something like NeuroExplorer will take some parameter tuning.
My guess...and it's totally a guess as I was once in neuroscience but now NeuroExplorer is a distant memory...is that the spectrogram is an average of the spectrograms for each trial (or event). If you averaged all of the waveforms first, and then took the spectrogram, waves with opposite phase would cancel out. Now it's certainly true that some of the coolest phenomena in neuroscience (IMO) are stimulus induced phase resets of the LFP (e.g.), but it's unlikely that this is what you're after and there'd be a better plot for that anyway.
So this is what I'd recommend:
  • Get the spectrogram of the first row, plot it.
  • Play with the parameters for 5 minutes to make sure they make some sense to you
  • Get the spectrogram of the second row, plot it
  • Average the two spectrograms, plot the average
  • Create a 3-D matrix which is [n,m,k] big, n x m are going to be the size of a single spectrogam and k is going to be the number of trials
  • Loop over trials, store each trial's spectrogram in your matrix
  • Take the mean over the third dimension (mean(myspecmatrix,3)) and plot it
Some tips for spectrogram:
  • if you call spectrogram with no outputs, it will plot. But the default parameters are unlikely to be good for neural data (8 bins), and spectrogram's plot is (oddly) frequency on the x axis and time on the y axis, which will probably be backwards for you
  • You'll want to specify all of the inputs for spectrogram: X is the signal (a trial in your case); WINDOW is the size of the bin that's going into the spectrum estimate, maybe you want a second or so here to start (really depends on what you're looking for), bigger bin means more frequency resolution but less time resolution; NOVERLAP is how much that window will slide by, maybe start with something 80% of WINDOW; NFFT can be difficult, it's the number of points used in the Fourier transform, you can use [ ] to let MATLAB pick but note that this will determine the size of your output; Fs is your sampling rate.
  • If you do [s,y,x] = spectrogram(...), then you can do imagesc(x,y,log10(abs(s))) to plot it. Adjusting the axes clim property is a good way to clean up the noise in this plot.
  2 Comments
Dave B
Dave B on 11 Nov 2021 at 13:28
in this case the x and y aren't changing from trial to trial. You could choose to store them once, or overwrite them on every loop. I think it's more simple just to keep overwriting them:
numTrials = size(data,1);
for i = 1:numTrials
[sGram(:,:,i), y, x] = spectrogram(data(i,:));
end

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by