F0 Spectrogram

1 view
Skip to first unread message

Earleen Statham

unread,
Aug 5, 2024, 2:36:39 AM8/5/24
to licasingche
Spectrogramsare used extensively in the fields of music, linguistics, sonar, radar, speech processing,[1] seismology, ornithology, and others. Spectrograms of audio can be used to identify spoken words phonetically, and to analyse the various calls of animals.

A spectrogram can be generated by an optical spectrometer, a bank of band-pass filters, by Fourier transform or by a wavelet transform (in which case it is also known as a scaleogram or scalogram).[2]


A common format is a graph with two geometric dimensions: one axis represents time, and the other axis represents frequency; a third dimension indicating the amplitude of a particular frequency at a particular time is represented by the intensity or color of each point in the image.


There are many variations of format: sometimes the vertical and horizontal axes are switched, so time runs up and down; sometimes as a waterfall plot where the amplitude is represented by height of a 3D surface instead of color or intensity. The frequency and amplitude axes can be either linear or logarithmic, depending on what the graph is being used for. Audio would usually be represented with a logarithmic amplitude axis (probably in decibels, or dB), and frequency would be linear to emphasize harmonic relationships, or logarithmic to emphasize musical, tonal relationships.


The bandpass filters method usually uses analog processing to divide the input signal into frequency bands; the magnitude of each filter's output controls a transducer that records the spectrogram as an image on paper.[3]


From the formula above, it appears that a spectrogram contains no information about the exact, or even approximate, phase of the signal that it represents. For this reason, it is not possible to reverse the process and generate a copy of the original signal from a spectrogram, though in situations where the exact initial phase is unimportant it may be possible to generate a useful approximation of the original signal. The Analysis & Resynthesis Sound Spectrograph[6] is an example of a computer program that attempts to do this. The Pattern Playback was an early speech synthesizer, designed at Haskins Laboratories in the late 1940s, that converted pictures of the acoustic patterns of speech (spectrograms) back into sound.


The size and shape of the analysis window can be varied. A smaller (shorter) window will produce more accurate results in timing, at the expense of precision of frequency representation. A larger (longer) window will provide a more precise frequency representation, at the expense of precision in timing representation. This is an instance of the Heisenberg uncertainty principle, that the product of the precision in two conjugate variables is greater than or equal to a constant (B*T>=1 in the usual notation).[8]


I am looking to plot a spectrogram of a signal, but I am running into some issues. I have created a minimal working example, and will walk through it. What follows is a bit messy, but I think that I clear question emerges at the end, and that showing the messy-ness is a good way of showing my confusion, which is the problem:


Can you see the skinny line down along the x-axis? That is the only content of the plot. So initially, the axis-scaling is all messed up. This has been the case for all 3 times I have tried, and every time, the data is squished up against the x-axis. But lets zoom in on the output we got:

image749499 61.8 KB


Now, I can make out that we have a frequency that rises with time, but the plot does look quite bad. The y-ticks are gone, and I can not hover to see the y-values. I also would prefer it to be continuos in colour instead of controur-lines. I have tried calling the last plot-command with heatmap instead of plot, which produces the following:

image742472 4.26 KB


I can zoom into this as well, but the resolution seems horrible, and I can not see what in m example should reduce :

image749500 9.09 KB

In addition there is no colorbar, which I have not been able to add.


That helps so much! Playing around with the numbers of samples per fft of the spectrogram made things more clear. The frequency-axis also extends from 0 to fs/2, and so setting y-lims to what I could tell to be the interesting parts based on an FFT of the whole signal turned out to be very helpful as well. It took some tinkering, but the result is great


, where data is a morse signal with some high frequency noise that fades in and out on top. The resulting spectrogram became

image748500 70.4 KB

, and one can see the morse signl and the noise very clearly.


NB:

The Wigner-Ville transform should achieve better time-frequency resolution. See this reference for a nice summary and comparison of different Time-Frequency transform methods.

For an old Julia implementation of the W-V transform, you may want to look here.


The Wigner-Ville transform should achieve better time-frequency resolution. See this reference for a nice summary and comparison of different Time-Frequency transform methods.

For an old Julia implementation of the W-V transform, you may want to look here.


You are getting this error as spectrogram requires an AbstractVector. The mono function you are using from SampledSignals returns a [Nx1] matrix. You can fix this using the vec function returning that as a vector instead.


So I'm having some trouble conceptually and am hoping somebody can clear something up for me. This is long and I apologize for that but I want to provide as much information as possible so that the source of my confusion might be exposed.


As I've shown above, I reasoned this out, but it seems that if I increased $N$ and thereby sampled at a higher rate, then I would have more samples per second in a half-second time frame and my frequency resolution should be higher! I mean after all, if I'm looking from 0 to 0.5 seconds, and didn't have to divide by $X$ because of the memory limit, then why wouldn't I have a higher frequency resolution? I would have more samples!


The frequency resolution stays the same because increasing the sampling rate increases the maximum frequency that can be resolved. For example, doubling the sampling rate would double the maximum frequency.


You should not be running out of memory - I routinely calculate spectrograms on data with millions of points, you just have to tweak the input parameters. For example, putting WINDOW = 1000 and NOVERLAP = 999 will give you one time point for every sample and the output spectrogram will be, in your case, 74767 wide! Try the above settings and see how it goes.


I am doing a project where I will be implementing a trained neural network (trained with Keras) onto a STM32F746-DISCOVERY board with X-Cube AI. The goal is to train the network to recognize audio samples converted into spectrograms. This would mean that on the microcontroller, I would need to convert the audio input into spectrogram images, and then input that into the neural network for recognition.


In FP-AI-SENSING1 v3.0.0, there is an STM32_AI_AudioPreprocessing_Library Middleware library that can be used exactly for this purpose. The library provides the building blocks for spectral analysis and feature extraction, such as:


Recalculating common tables is only required if you change some preprocessing parameters and you want to avoid going through the MelFilterbank_Init, and Window_Init. The lookup tables stored in common_tables.h are for a given configuration. If you are using different preprocessing parameters, these lookup tables can be created at runtime in RAM using the _Init() functions. This is not the case in FP-AI-SENSING1. The preprocessing lookup tables have been generated offline and stored in ROM Flash using common_tables.c


This sounds very strange to me, and I can't get my brains around it. I don't think I understand what "slices" are or what it means for "time to seem continuous" in the spectrogram itself. Can someone help me come to terms with this?


Everything about sound in the digital world presents discrete (not continuous) values which represent "what is happening in this (physically continuous) time span out there". It is impossible to compute amplitude at a given frequency and a single instant in time (for one, there is no such thing as an absolute "instant"). When you decompose a waveform into a graph of frequency and amplitude, you have to select a specific continuous chunk of the waveform, in order to ferret patterns of frequent amplitude changes, versus infrequent amplitude changes. A Fourier spectrum uses the concept of a "window" and throws away any information about when in time a given component occurred; instead, it gives you the amplitude at a given frequency, for the entire signal between time n and time n+j. This can be displayed, and gives you a very accurate representation of frequency and amplitude within that window. This is a spectral "slice" -- the frequency and amplitude values derived from the Fourier transform of the signal at some (small) interval.


A spectrogram is created by computing a frequency-and-amplitude display (which has no time component) for very many adjacent (partially overlapping) windows of time, and until we get 3-D view screens, amplitude information has to be degraded by translating it into scales of grey, so that you are effectively looking at a single spectrum edge-on.


Good answers!A simpler way to look at it might be that frequencies only exist over time, never in an instant. So, if you analyze a waveform for frequencies, you have to decide over what period of time to analyze it. If you choose 10 milliseconds, you'll get an average strength for the various frequencies as they appear in those 10 milliseconds, but you don't know how strong each frequency component is for each millisecond. If you use 1 millisecond, you get much more accuracy timewise, but you will know fewer frequency components. Crazy to think that the human brain isolates frequencies in sound all the time, albeit not always accurately!

3a8082e126
Reply all
Reply to author
Forward
0 new messages