Bohrium
robot
新建

空间站广场

论文
Notebooks
比赛
课程
Apps
我的主页
我的Notebooks
我的论文库
我的足迹

我的工作空间

任务
节点
文件
数据集
镜像
项目
数据库
公开
Digital-Signal Process_02 Spectral Analysis of Deterministic Signals_Short-Time Fourier Transform
English
Signal Processing
notebook
EnglishSignal Processingnotebook
喇叭花
发布于 2023-08-02
推荐镜像 :digital-signal-processing:Digital-Signal-Processing-Lecture
推荐机型 :c2_m4_cpu
Spectral Analysis of Deterministic Signals
Short-Time Fourier Transform
The Spectrogram

Spectral Analysis of Deterministic Signals

This jupyter notebook is part of a collection of notebooks on various topics of Digital Signal Processing. Please direct questions and suggestions to Sascha.Spors@uni-rostock.de.

代码
文本

🏃🏻 Getting Started Guide
This document can be executed directly on the Bohrium Notebook. To begin, click the Connect button located at the top of the interface, then select the digital-signal-processing:Digital-Signal-Processing-Lecture Image and choose your desired machine configuration to proceed.

📖 Source
This Notebook is from a famous Signal Processing Lecture.

代码
文本

Short-Time Fourier Transform

The discrete Fourier transform (DFT) is not very well suited for the analysis of instationary signals when applied to the entire signal at once. Furthermore practical signals, for instance, an antenna signal, cannot be analyzed in an on-line manner by the DFT. This motivates to split a long signal into segments and compute the DFT on these segments. This transform is known as the short-time Fourier transform (STFT).

The STFT of a signal is defined as

where denotes a window function of length which is normalized by . Starting from , the signal is windowed by to a segment of length . This windowed segment is then transformed by a DFT of length .

The STFT has many applications in digital signal processing, for instance, in the spectral analysis of signals or the processing of instationary signals. The resulting spectrum depends on the frequency index and the time index . The spectral domain is therefore also termed as time-frequency domain and techniques using the STFT as time-frequency processing.

The properties of the STFT depend on

  • the length of the segments,
  • the overlap between the segments, and
  • the window function .

The size of the segments and the window function influence the spectral and temporal resolution of the STFT. The time index of the STFT can be increased by an arbitrary step size. The step size determines the overlap between two consecutive STFTs. For instance, the spectra and have overlapping samples. The overlap is sometimes given as percentage of the segment length .

代码
文本

The Spectrogram

The magnitude of the STFT is known as the spectrogram of a signal. It is frequently used to analyze signals in the time-frequency domain, for instance by a spectrum analyzer.

代码
文本

Example - Spectrogram of a chirp/sweep signal

The following example compares the Fourier transform and spectrogram of a chirp/sweep signal. A chirp signal is a signal whose instantaneous frequency increases or decreases with time. Lets first listen to a chirp signal whose instantaneous frequency increases linear with time

/data/linear_sweep.wav

Now the magnitude spectrum of a chirp signal is computed and plotted

代码
文本
[1]
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as sig


N = 8192 # length of the signal

# generate signal
k = np.arange(N)
x = sig.chirp(k, 0.01, N, .49)

# compute and plot magnitude spectrum
plt.figure(figsize=(10, 3))
f = np.fft.rfftfreq(N, 1/2)
plt.plot(f, 20*np.log10(abs(np.fft.rfft(x))))
plt.xlabel(r'$\Omega$ / $\pi$')
plt.ylabel(r'$|X[\Omega]|$ in dB')
plt.ylim([0, 50])
plt.grid()
代码
文本

Followed by the spectrogram of the chirp signal

代码
文本
[2]
L = 256 # length of one segment
overlap = 128 # overlap between segments

plt.figure(figsize=(10, 5))
plt.specgram(x, NFFT=L, Fs=2, noverlap=overlap, sides='onesided')
plt.xlabel(r'$n$')
plt.ylabel(r'$\Omega$ / $\pi$')
cb = plt.colorbar()
cb.set_label(r'$|X[\Omega,n]|$ in dB')
plt.autoscale(tight=True)
代码
文本

Exercise

  • Which spectral properties of the chirp signal can be concluded from the magnitude spectrum and the spectrogram?
  • Change the segment length L of the spectrogram and the overlap overlap between segments. Rerun the example. What changes?
  • Change the window function used for the spectrogram by extending the call to plt.specgram by the parameter window. For instance add window = np.ones(L) for the rectangular window.

Solution: The magnitude spectrum, which has been computed from the entire signal, shows only that the overall spectrum of the chirp is approximately constant. The temporal evolution of the instantaneous frequency of the chirp signal can only be observed in the spectrogram. It can be concluded that the instantaneous frequency increases linear with time. A higher segment length leads to a narrower main lobe and thus better visibility of the current frequency. With a higher overlap, the spectrum has a higher temporal resolution.

代码
文本

Example - Spectrogram of a speech signal

The following example computes the spectrogram of a speech signal containing a short sentence. Lets first listen to the speech signal

/data/speech_8k.wav

The signal is loaded using the soundfile module for Python.

代码
文本
[3]
import soundfile as sf

L = 1024 # length of one segment
overlap = 512 # overlap between segments

# read speech signal from file
x, fs = sf.read('/digital-signal-processing-lecture/data/speech_8k.wav')
x = x/np.max(np.abs(x))

# compute and plot spectrogram
plt.figure(figsize=(10, 5))
plt.specgram(x, NFFT=L, Fs=fs, noverlap=overlap, sides='onesided')
plt.xlabel(r'$n$ in s')
plt.ylabel(r'$f$ in Hz')
cb = plt.colorbar()
cb.set_label(r'$|X[f,n]|$ in dB')
plt.autoscale(tight=True)
plt.ylim([0, 2000])
(0.0, 2000.0)
代码
文本

Exercise

  • A speech signal exhibits a specific spectral structure. What insights can you gain from the spectrum above?
  • Change the segment length and overlap of the STFT. How does the spectrogram change?

Solution: In case of a vowel a speech signal is periodic due to the underlying speech production mechanism. A periodic signal has a line spectrum consisting of a fundamental frequency and various harmonics with multiple of the fundamental frequency. The harmonic structure of the vowels is visible in the spectrogram.

代码
文本

Copyright

This notebook is provided as Open Educational Resource. Feel free to use the notebook for your own purposes. The text is licensed under Creative Commons Attribution 4.0, the code of the IPython examples under the MIT license. Please attribute the work as follows: Sascha Spors, Digital Signal Processing - Lecture notes featuring computational examples.

代码
文本
English
Signal Processing
notebook
EnglishSignal Processingnotebook
点个赞吧
推荐阅读
公开
Digital-Signal Processing_02_Spectral Analysis of Deterministic Signals_Summary
EnglishnotebookSignal Processing
EnglishnotebookSignal Processing
喇叭花
发布于 2023-08-02
1 转存文件
公开
Digital-Signal Processing 02_Spectral Analysis of Deterministic Signals_The Leakage Effect
EnglishnotebookSignal Processing
EnglishnotebookSignal Processing
喇叭花
发布于 2023-08-02