# Calculate spectrogram with logarithmic-spaced frequencies

26 views (last 30 days)
dormant on 21 Dec 2022
Commented: William Rose on 23 Dec 2022
I'm trying to calculate a spectrogram where the frequencies are logarithmicaly spaced, instead of the usual linear.
I think my test script follows the documentation, but my two spectrograms are identical and are both clearly using linear frequency spacing.
Am I doing something wrong?
Attached is my test script and the data.
dormant on 21 Dec 2022
Unfortunately, the code I provided doesn't run, because I forgot to specify one variable that is normally read in. But I appreciate your comments.

dormant on 21 Dec 2022
Edited: dormant on 21 Dec 2022
After some more research, I think it's impossible to do what I want with the spectrogram function. I think it only works with a vector of linear-spaced frequencies.
I think I have to do what is called a Time Frequency Representation (TFR). I have got some python code that does this but it's beyond my comprehension. I've found some old MATLAB scripts (e.g. http://www.physics.lancs.ac.uk/research/nbmphysics/diats/tfr/).
The MATLAB cwt function (in the wavelet toolbox) seems to do what I want to do, so I'll try and explore that.

William Rose on 21 Dec 2022
@dormant, I agree that the bottom plot in your example, which should have the frequencies evenly spaced on a log scale, appears to have frequencies that are evenly spaced on a linear scale, contrary to what you expect and what you want. The help for spectrogram, for the "f" option, says "Cyclical frequencies, specified as a vector. f must have at least two elements, because otherwise the function interprets it as nfft. The units of f are specified by the sample rate, fs." I think this means f should be specified as frequencies normalized by fs. (Which is interesting, since Matlab's filtering routines normalize frequencies by the Nyquist frequency.) Your specification of f is not normalized by fs. Perhaps your f vector is ignored because the values are not all less than 1, which they should be, according to the help quoted above.
William Rose on 23 Dec 2022
@dormant, Thank you for shairng your results. I'm glad you like the scalogram.
Even with the scalogram, the frequencies still appear to be linearly spaced, not log-spaced. I say this because the scalogram plot with a logarithmic frequency axis shows finer divisions at higher frequencies than at lower frequencies. In this respect the scalogram plot is not diferent than the spectrogram plots in your original post, which you did not like, due to the linear spacing of frequency samples.
I agree with you that the scalogram appears to have better resolution (better time-bandwidth product) than the spectrogram. I would have to compare spectrograms and scalograms of signals with simple, well defined time-varying frequency content, before having confidence in this conclusion. I am not fond of wavelets due to the difficulty in interpreting the results, and because the mother wavelet choice is somewhat arbitrary and will influence the results.

### Categories

Find more on Time-Frequency Analysis in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by