MATLAB 'spectrogram' params - matlab

I am a beginner in MATLAB and I should perform a spectral analysis of an EEG signal drawing the graphs of power spectral density and spectrogram. My signal is 10 seconds long and a sampling frequency of 160 Hz, a total of 1600 samples and have some questions on how to find the parameters of the functions in MATLAB, including:
pwelch (x, window, noverlap, nfft, fs);
spectrogram (x, window, noverlap, F, fs);
My question then is where to find values ​​for the parameters window and noverlap I do not know what they are for.

To understand window functions & their use, let's first look at what happens when you take the DFT of finite length samples. Implicit in the definition of the discrete Fourier transform, is the assumption that the finite length of signal that you're considering, is periodic.
Consider a sine wave, sampled such that a full period is captured. When the signal is replicated, you can see that it continues periodically as an uninterrupted signal. The resulting DFT has only one non-zero component and that is at the frequency of the sinusoid.
Now consider a cosine wave with a different period, sampled such that only a partial period is captured. Now if you replicate the signal, you see discontinuities in the signal, marked in red. There is no longer a smooth transition and so you'll have leakage coming in at other frequencies, as seen below
This spectral leakage occurs through the side-lobes. To understand more about this, you should also read up on the sinc function and its Fourier transform, the rectangle function. The finite sampled sequence can be viewed as an infinite sequence multiplied by the rectangular function. The leakage that occurs is related to the side lobes of the sinc function (sinc & rectangular belong to self-dual space and are F.Ts of each other). This is explained in more detail in the spectral leakage article I linked to above.
Window functions
Window functions are used in signal processing to minimize the effect of spectral leakages. Basically, what a window function does is that it tapers the finite length sequence at the ends, so that when tiled, it has a periodic structure without discontinuities, and hence less spectral leakage.
Some of the common windows are Hanning, Hamming, Blackman, Blackman-Harris, Kaiser-Bessel, etc. You can read up more on them from the wiki link and the corresponding MATLAB commands are hann, hamming,blackman, blackmanharris and kaiser. Here's a small sample of the different windows:
You might wonder why there are so many different window functions. The reason is because each of these have very different spectral properties and have different main lobe widths and side lobe amplitudes. There is no such thing as a free lunch: if you want good frequency resolution (main lobe is thin), your sidelobes become larger and vice versa. You can't have both. Often, the choice of window function is dependent on the specific needs and always boils down to making a compromise. This is a very good article that talks about using window functions, and you should definitely read through it.
Now, when you use a window function, you have less information at the tapered ends. So, one way to fix that, is to use sliding windows with an overlap as shown below. The idea is that when put together, they approximate the original sequence as best as possible (i.e., the bottom row should be as close to a flat value of 1 as possible). Typical values vary between 33% to 50%, depending on the application.
Using MATLAB's spectrogram
The syntax is spectrogram(x,window,overlap,NFFT,fs)
where
x is your entire data vector
window is your window function. If you enter just a number, say W (must be integer), then MATLAB chops up your data into chunks of W samples each and forms the spectrogram from it. This is equivalent to using a rectangular window of length W samples. If you want to use a different window, provide hann(W) or whatever window you choose.
overlap is the number of samples that you need to overlap. So, if you need 50% overlap, this value should be W/2. Use floor(W/2) or ceil(W/2) if W can take odd values. This is just an integer.
NFFT is the FFT length
fs is the sampling frequency of your data vector. You can leave this empty, and MATLAB plots the figure in terms of normalized frequencies and the time axis as simply the data chunk index. If you enter it, MATLAB scales the axis accordingly.
You can also get optional outputs such as the time vector and frequency vector and the power spectrum computed, for use in other computations or if you need to style your plot differently. Refer to the documentation for more info.
Here's an example with 1 second of a linear chirp signal from 20 Hz to 400 Hz, sampled at 1000 Hz. Two window functions are used, Hanning and Blackman-Harris, with and without overlaps. The window lengths were 50 samples, and overlap of 50%, when used. The plots are scaled to the same 80dB range in each plot.
You can notice the difference in the figures (top-bottom) due to the overlap. You get a cleaner estimate if you use overlap. You can also observe the trade-off between main lobe width and side lobe amplitude that I mentioned earlier. Hanning has a thinner main lobe (prominent line along the skew diagonal), resulting in better frequency resolution, but has leaky sidelobes, seen by the bright colors outside. Blackwell-Harris, on the other hand, has a fatter main lobe (thicker diagonal line), but less spectral leakage, evidenced by the uniformly low (blue) outer region.

Both these methods above are short-time methods of operating on signals. The non-stationarity of the signal (where statistics are a function of time, Say mean, among other statistics, is a function of time) implies that you can only assume that the statistics of the signal are constant over short periods of time. There is no way of arriving at such a period of time (for which the statistics of the signal are constant) exactly and hence it is mostly guess work and fine-tuning.
Say that the signal you mentioned above is non-stationary (which EEG signals are). Also assume that it is stationary only for about 10ms or so. To reliably measure statistics like PSD or energy, you need to measure these statistics 10ms at a time. The window-ing function is what you multiply the signal with to isolate that 10ms of a signal, on which you will be computing PSD etc.. So now you need to traverse the length of the signal. You need a shifting window (to window the entire signal 10ms at a time). Overlapping the windows gives you a more reliable estimate of the statistics.
You can imagine it like this:
1. Take the first 10ms of the signal.
2. Window it with the windowing function.
3. Compute statistic only on this 10ms portion.
4. Move the window by 5ms (assume length of overlap).
5. Window the signal again.
6. Compute statistic again.
7. Move over entire length of signal.
There are many different types of window functions - Blackman, Hanning, Hamming, Rectangular. That and the length of the window and overlap really depend on the application that you have and the frequency characteristics of the signal itself.
As an example, in speech processing (where the signals are non-stationary and windowing gets used a lot), the most popular choices for windowing functions are Hamming/Hanning of length 10ms (320 samples at 16 kHz sampling) with an overlap of 80 samples (25% of window length). This works reasonably well. You can use this as a starting point for your application and then work on fine-tuning it a little more with different values.
You may also want to take a look at the following functions in MATLAB:
1. hamming
2. hanning
I hope you know that you can call up a ton of help in MATLAB using the help command on the command line. MATLAB is one of the best documented softwares out there. Using the help command for pwelch also pulls up definitions for window size and overlap. That should help you out too.
I don't know if all this info. helped you out or not, but looking at the question, I felt you might have needed a little help with understanding what windowing and overlapping was all about.
HTH,
Sriram.

For the last parameter fs, that is the frequency rate of the raw signal, in your case X, when you extract X from audio data using function
[X,fs]=audioread('song.mp3')
You may get fs from it.

Investigate how the following parameters change the performance of the Sinc function:
The Length of the coefficients
The Following window functions:
Blackman Harris
Hanning
Bartlett

Related

How to convert scales to frequencies in Wavelet Transform

I'm dealing with CWT, and I have a big problem converting scales to frequencies. In the MAtlab Wavelet Tutorial they use this expression to convert scales to frequencies
But if i use the default function scal2freq I obtain different result.
I don't understand the role of the Morlet Fourier Factor
Thanks in advance
It is a pretty complicated concept, which I somewhat understand it. I'll write some points here so that you might figure it out yourself, rather easier.
A simple fact is that:
Scale is inversely proportional to frequency.
For example, imagine we have a 1-100 Hz range of frequencies in some time series data such as stock markets data or earthquake data. Scale is "supposed to be" the inverse of that. For instance, if scale would be in range of 1 to 100, we'd have had:
Scale(1/Hz) Frequency (Hz)
1 100
50 50
100 1
Therefore,
The frequency is not the real frequency of those time series data (e.g., stock market, earthquake) that we know of. They are only related, inversely.
And we can safely say that here we are calculating some "pseudo-frequencies", which MATLAB does that (by approximating that). You can read about the approximation process in the documentation in the section pseudo-frequencies:
MATlAB does calculate those pseudo-frequencies based on:
In wavelet analysis, the way to relate scales to frequencies is to determine the center frequency of the wavelet function:
which you can visually see in this image and of-course it would differ, when we would change the types of our function in the calculation. Thus, that center frequency will change everytime in our approximation process:
That "MorletFourierFactor" is a variable to approximate a constant so that when you would do the 1/scale, it would closely approximate those "pseudo-frequencies".
I thought this image about shifting (time axis) and scaling (frequency axis) might be a little helpful to look into as well:
The bottom line is that don't worry about pseudo-frequencies, you wouldn't probably need those. If you would want any frequency spectrum, you can likely go towards applying some of those frequency methods (such as Fast Fourier Transform) on whatever time series data that you have.
If you really really want to map that, you can also try to design some methods to approximate it yourself.
Source
Harvard Seismology

Estimating Quasi-stationary part of a signal

I am trying to estimate the Estimating Quasi-stationary part of a signal in Matlab. It is a 1 second long sound signal that belongs to a bird.
I am using MFCC to extract features but would like to have a window size for MFCC that is guaranteed to operate on statistically quasi-stationary part.
My questions are:
Do you think it is a solid approach if I iterate by varying my window size from 1 second to a smaller interval by observing the change of second moment of features and making a decision where the second moment is not changing anymore?
If I use Shannon entropy method by again varying my MFCC window size, how the number of bits I got at the output of the entropy algorithm would help me to identify the Estimating Quasi-stationary part of the signal
Are there any other ideas?

MATLAB, averaging multiple fft's ,coherent integration

I have audio record.
I want to detect sinusoidal pattern.
If i do regular fft i have result with bad SNR.
for example
my signal contents 4 high frequencies:
fft result:
To reduce noise i want to do Coherent integration as described in this article: http://flylib.com/books/en/2.729.1.109/1/
but i cant find any MATLAB examples how to do it. Sorry for bad english. Please help )
I look at spectra almost every day, but I never heard of 'coherent integration' as a method to calculate one. As also mentioned by Jason, coherent integration would only work when your signal has a fixed phase during every FFT you average over.
It is more likely that you want to do what the article calls 'incoherent integration'. This is more commonly known as calculating a periodogram (or Welch's method, a slightly better variant), in which you average the squared absolute value of the individual FFTs to obtain a power-spectral-density. To calculate a PSD in the correct way, you need to pay attention to some details, like applying a suitable Fourier window before doing each FFT, doing the proper normalization (so that the result is properly calibrated in i.e. Volt^2/Hz) and using half-overlapping windows to make use of all your data. All of this is implemented in Matlab's pwelch function, which is part of the signal-processing toolbox. See my answer to a similar question about how to use pwelch.
Integration or averaging of FFT frames just amounts to adding the frames up element-wise and dividing by the number of frames. Since MATLAB provides vector operations, you can just add the frames with the + operator.
coh_avg = (frame1 + frame2 + ...) / Nframes
Where frameX are the complex FFT output frames.
If you want to do non-coherent averaging, you just need to take the magnitude of the complex elements before adding the frames together.
noncoh_avg = (abs(frame1) + abs(frame2) + ...) / Nframes
Also note that in order for coherent averaging to work the best, the starting phase of the signal of interest needs to be the same for each FFT frame. Otherwise, the FFT bin with the signal may add in such a way that the amplitudes cancel out. This is usually a tough requirement to ensure without some knowledge of the signal or some external triggering so it is more common to use non-coherent averaging.
Non-coherent integration will not reduce the noise power, but it will increase signal to noise ratio (how the signal power compares to the noise power), which is probably what you really want anyway.
I think what you are looking for is the "spectrogram" function in Matlab, which computes the short time Fourier transform(STFT) of an input signal.
STFT
Spectrogram

Noisy signal correlation

I have two (or more) time series that I would like to correlate with one another to look for common changes e.g. both rising or both falling etc.
The problem is that the time series are all fairly noisy with relatively high standard deviations meaning it is difficult to see common features. The signals are sampled at a fairly low frequency (one point every 30s) but cover reasonable time periods 2hours +. It is often the case that the two signs are not the same length, for example 1x1hour & 1x1.5 hours.
Can anyone suggest some good correlation techniques, ideally using built in or bespoke matlab routines? I've tried auto correlation just to compare lags within a single signal but all I got back is a triangular shape with the max at 0 lag (I assume this means there is no obvious correlation except with itself?) . Cross correlation isn't much better.
Any thoughts would be greatly appreciated.
Start with a cross-covariance (xcov) instead of the cross-correlation. xcov removes the DC component (subtracts off the mean) of each data set and then does the cross-correlation. When you cross-correlate two square waves, you get a triangle wave. If you have small signals riding on a large offset, you get a triangle wave with small variations in it.
If you think there is a delay between the two signals, then I would use xcorr to calculate the delay. Since xcorr is doing an FFT of the signal, you should remove the means before calling xcorr, you may also want to consider adding a window (e.g. hanning) to reduce leakage if the data is not self-windowing.
If there is no delay between the signals or you have found and removed the delay, you could just average the two (or more) signals. The random noise should tend to average to zero and the common features will approach the true value.

Signal processing using MATLAB

I'm trying to do some signal processing using an audio file (piano recordings)
I find the note onsets and then perform FFT on each onset. However I find that for certain notes their 2nd harmonic has a way greater amplitude than he fundamental... Why is that???
How can I eliminate this and get the correct frequency??
Start by using a low-pass filter to trim out some of the higher-order harmonics. If the piano recordings that you are trying to process were recorded within a 3 octave range, that should help substantially.
Next, try adjusting your wave amplitude. Here's an article that discusses how harmonic distortion degrades a signal, and how you can exchange signal-to-noise ratio for harmonic distortion.
http://www.mathworks.com/help/signal/examples/analyzing-harmonic-distortion.html
If you want more of a home-built solution without signal filtering, here's what I'd try, assuming that the maximum signal amplitude corresponds either to the fundamental, 2nd harmonic, or 3rd harmonic
1) Find the frequency f of the maximum signal
2) If the signal at f/2 or f/3 is much greater than the noise floor, call that frequency your fundamental
Alternatively,
1) Find the frequency f of the maximum signal
2) Search above in the interval [f/2, 2*f] and find the peak nearest f.
3) Assume the difference between f and the nearest peak is 1 the fundamental frequency.
You'll need to adapt these methods to your data.
Make sure your data doesn't exhibit only odd order harmonics or has very strong high-order harmonics. These methods won't work well if multiple notes are played simultaneously.
You could also try correcting your data for human ear sensitivity, as that may be the reason why the 2nd harmonics are louder on an FFT than what the ear detects relative to the fundamental. See http://en.wikipedia.org/wiki/Absolute_threshold_of_hearing