MATLAB“频谱图”参数
我是 MATLAB 的初学者,我应该对 EEG 信号进行频谱分析,绘制功率谱密度图和频谱图。我的信号长 10
秒,采样频率为 160 Hz
,总共 1600 个样本
,有一些关于如何查找参数的问题MATLAB 中的函数,包括:
pwelch (x, window, noverlap, nfft, fs);
spectrogram (x, window, noverlap, F, fs);
我的问题是在哪里可以找到参数 window
和 noverlap
的值,我不知道它们的用途。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
了解窗口函数和窗口函数为了了解它们的用途,我们首先看看对有限长度样本进行 DFT 时会发生什么。离散傅立叶变换的定义中隐含的假设是您正在考虑的信号的有限长度是周期性的。
考虑一个正弦波,对其进行采样以捕获完整周期。当信号被复制时,您可以看到它作为不间断信号周期性地持续。生成的 DFT 仅具有一个非零分量,即正弦曲线的频率。
现在考虑具有不同周期的余弦波,采样后仅捕获部分周期。现在,如果您复制信号,您会看到信号中的不连续性,以红色标记。不再平滑过渡,因此您会在其他频率出现泄漏,如下所示
这种频谱泄漏是通过旁瓣发生的。要了解更多信息,您还应该阅读 sinc 函数 及其傅里叶变换,矩形函数。有限采样序列可以看作是无限序列乘以矩形函数。发生的泄漏与sinc函数的旁瓣有关(sinc和矩形属于自对偶空间,互为F.T)。我在上面链接的频谱泄漏文章中对此进行了更详细的解释。
窗函数
窗函数用于信号处理,以最大限度地减少频谱泄漏的影响。基本上,窗函数的作用是在末端逐渐缩小有限长度序列,以便在平铺时,它具有没有不连续性的周期性结构,因此频谱泄漏更少。
一些常见的窗口有 Hanning、Hamming、Blackman、Blackman-Harris、Kaiser-Bessel 等。您可以从 wiki 链接阅读有关它们的更多信息,相应的 MATLAB 命令为
hann
、汉明
、布莱克曼
、blackmanharris
和凯撒
。以下是不同窗口的一个小示例:您可能想知道为什么有这么多不同的窗口函数。原因是因为它们中的每一个都具有非常不同的频谱特性,并且具有不同的主瓣宽度和旁瓣幅度。天下没有免费的午餐:如果您想要良好的频率分辨率(主瓣很薄),您的旁瓣就会变大,反之亦然。你不能两者兼得。通常,窗函数的选择取决于具体需求,并且总是要做出妥协。 这是一篇非常好的文章,讨论了使用窗口函数,你一定要通读它。
现在,当您使用窗口函数时,锥形末端的信息会减少。因此,解决这个问题的一种方法是使用重叠的滑动窗口,如下所示。这个想法是,当放在一起时,它们尽可能接近原始序列(即,底行应尽可能接近平坦值 1)。典型值在 33% 到 50% 之间变化,具体取决于应用。
使用 MATLAB 的频谱图
语法为
spectrogram(x,window,重叠,NFFT,fs)
其中
x
是您的整个数据向量window
是您的窗口函数。如果您只输入一个数字,例如W
(必须是整数),那么 MATLAB 会将您的数据分割成W
样本块,并从中形成频谱图。这相当于使用长度为W
个样本的矩形窗口。如果您想使用不同的窗口,请提供hann(W)
或您选择的任何窗口。overlap
是需要重叠的样本数。因此,如果您需要 50% 的重叠,则该值应为W/2
。如果W
可以取奇数值,则使用floor(W/2)
或ceil(W/2)
。这只是一个整数。NFFT
是 FFT 长度fs
是数据向量的采样频率。您可以将其留空,MATLAB 将根据归一化频率和时间轴绘制图形,并将其简单地作为数据块索引。如果输入它,MATLAB 会相应地缩放轴。您还可以获得可选的输出,例如时间向量和频率向量以及计算的功率谱,以用于其他计算或如果您需要以不同的方式设计绘图。有关详细信息,请参阅文档。
下面是一个 1 秒线性调频信号的示例,频率范围为 20 Hz 至 400 Hz,采样频率为 1000 Hz。使用两个窗口函数,
Hanning
和Blackman-Harris
,有重叠和没有重叠。使用时,窗口长度为 50 个样本,重叠率为 50%。每个图中的图都缩放至相同的 80dB 范围。您可以注意到由于重叠而导致的数字(上下)差异。如果使用重叠,您会得到更清晰的估计。您还可以观察我之前提到的主瓣宽度和旁瓣幅度之间的权衡。汉宁具有较薄的主瓣(沿倾斜对角线的突出线),从而产生更好的频率分辨率,但具有泄漏的旁瓣,从外部明亮的颜色可以看出。另一方面,Blackwell-Harris 具有较宽的主瓣(较粗的对角线),但频谱泄漏较少,一致的低(蓝色)外部区域证明了这一点。
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
andkaiser
. 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 vectorwindow
is your window function. If you enter just a number, sayW
(must be integer), then MATLAB chops up your data into chunks ofW
samples each and forms the spectrogram from it. This is equivalent to using a rectangular window of lengthW
samples. If you want to use a different window, providehann(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 beW/2
. Usefloor(W/2)
orceil(W/2)
ifW
can take odd values. This is just an integer.NFFT
is the FFT lengthfs
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
andBlackman-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.
上述两种方法都是对信号进行短时操作的方法。信号的非平稳性(其中统计量是时间的函数,在其他统计量中,平均值是时间的函数)意味着您只能假设信号的统计量在短时间内保持不变。没有办法准确地达到这样一个时间段(信号的统计数据是恒定的),因此它主要是猜测和微调。
假设您上面提到的信号是非平稳的(脑电图信号是非平稳的)。还假设它仅静止约 10 毫秒左右。要可靠地测量 PSD 或能量等统计数据,您需要一次测量这些统计数据 10 毫秒。加窗函数是您将信号相乘以隔离信号的 10ms,您将在其上计算 PSD 等。所以现在您需要遍历信号的长度。您需要一个移动窗口(一次对整个信号进行 10 毫秒的窗口)。重叠窗口可以让您对统计数据进行更可靠的估计。
你可以这样想象:
1. 获取信号的前 10ms。
2. 使用窗口功能对其进行窗口化。
3. 只计算这10ms部分的统计数据。
4. 将窗口移动 5ms(假设重叠长度)。
5. 再次对信号加窗。
6. 再次计算统计量。
7. 在信号的整个长度上移动。
有许多不同类型的窗函数 -
Blackman、Hanning、Hamming、矩形
。这以及窗口和重叠的长度实际上取决于您所拥有的应用程序以及信号本身的频率特性。例如,在语音处理(其中信号是非平稳的并且经常使用加窗)中,最流行的加窗函数选择是长度
10ms 的 Hamming/Hanning(16 kHz 采样时有 320 个样本), 80 个样本的重叠(窗口长度的 25%)
。这相当有效。您可以使用它作为应用程序的起点,然后使用不同的值对其进行微调。您可能还想查看 MATLAB 中的以下函数:
1.
汉明
2.
hanning
我希望您知道,您可以使用命令行上的 help 命令在 MATLAB 中调用大量帮助信息。 MATLAB 是目前记录最好的软件之一。使用 pwelch 的帮助命令还可以获取窗口大小和重叠的定义。那也应该对你有帮助。
我不知道这些信息是否全部。不管是否对你有帮助,但看看这个问题,我觉得你可能需要一些帮助来理解窗口和重叠的含义。
HTH,
斯里拉姆。
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.
对于最后一个参数fs,即原始信号的频率,在您的情况下是X,当您使用函数从音频数据中提取X时,
您可以从中得到fs。
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
You may get fs from it.
研究以下参数如何改变 Sinc 函数的性能:
Investigate how the following parameters change the performance of the Sinc function: