如何在Python中加速信号处理
This post is the eighth installment of the series of articles on the RAPIDS ecosystem. The series explores and discusses various aspects of RAPIDS that allow its users solve ETL (Extract, Transform, Load) problems, build ML (Machine Learning) and DL (Deep Learning) models, explore expansive graphs, process signal and system log, or use SQL language via BlazingSQL to process data.
You may or may not be aware that every bit of information your computer has received from a server miles away, every pixel your screen has shown, or every tune your speakers has produced was some form of a signal that was sent over a ‘wire’. That signal was most likely encoded by the sender end so it could carry the information and the receiver side decoded it for further usage.
本文介绍RAPIDS生态系统系列。该系列探讨并讨论了RAPIDS的各个方面,这些方面使RAPIDS的用户可以解决ETL(提取,转换,加载)问题,构建ML(机器学习)和DL(深度学习)模型,探索膨胀图,过程信号和系统日志,或者通过BlazingSQL使用SQL语言处理数据。
可能会或可能不会意识到,计算机从一英里之外的服务器接收到的每点信息,屏幕上显示的每个像素或扬声器产生的每个音调都是通过“电线”发送的某种信号。该信号最有可能是由发送方编码的,因此可以携带信息,而接收方则对其进行解码以备将来使用。
信号丰富:音频,无线电或其它电磁波(例如伽马,红外或可见光),无线通信,海浪等。这些波浪中有一些是人造的,许多是自然产生的。甚至图像或股市时间序列也可以被视为信号。
cuSignal是RAPIDS库生态系统的更新版本。旨在分析和处理任何形式的信号,并在scikit-learn信号库之后进行了紧密建模。与scikit-learn不同,cuSignal将NVIDIA GPU的功能带到信号处理中,从而导致计算速度提高了几个数量级。
Signals are abundant: audio, radio or other electromagnetic waves (like gamma, infrared or visible light), wireless communications, ocean wave, and so on. Some of these waves are man-made, many are produced naturally. Even images or stock market time series can be seen and processed as signals.
cuSignal is a newer addition to the RAPIDS ecosystem of libraries. It is aimed at analyzing and processing signals in any form and is modeled closely after the scikit-learn signal library. However, unlike scikit-learn, cuSignal brings the power of NVIDIA GPUs to signal processing resulting in orders-of-magnitude increase in speed of computations.
In this post, we will introduce and showcase the most common functionality of RAPIDS cuSignal. As with the other libraries we already discussed, to help with getting familiar with cuSignal, we provide a cheat sheet that can be downloaded here: cuSignal cheatsheet, and an interactive notebook with all the current functionality of cuSignal showcased.
本文将介绍并展示RAPIDS cuSignal的最常用功能。与已经讨论过的其它库一样,为了帮助熟悉cuSignal,提供了一个备忘单,可以下载:cuSignal备忘单,以及展示 cuSignal所有当前功能的交互式笔记本。
Frequency
One of the most fundamental properties of signals is frequency. Hertz (abbreviated Hz) is a fundamental unit of frequency defined as a single cycle per second; it was named after Heindrich Rudolf Hertz who provided conclusive proof of the existence of electromagnetic waves. Any signal we detect or store is closely related to time: you could probably safely argue that any signal is a time series with ‘slightly’ different tools to analyze it.
The Alternating Current (AC) supplied to each home is an electric current that oscillates at either 50Hz or 60Hz, audio signals normally cover roughly the spectrum between 20Hz – 20,000Hz (or 20kHz), mobile bands cover some narrow bands in 850-900MHz, 1800Mhz (1.8GHz) and 1900MHz, Wifi signals oscillate at some predefined frequencies around either 2.4GHz or 5GHz. And these are but a few examples of signals that surround us. Ever heard of radio telescopes? The Wilkinson Microwave Anisotropy Probe is capable of scanning the night sky and detecting signals centered around 5 high-frequency bands: 23 GHz, 33 GHz, 41 GHz, 61 GHz, and 94 GHz, helping us to understand the beginnings of our universe. However, this is still just in the middle of the spectrum of electromagnetic waves.
信号的最基本特性之一是频率。赫兹(缩写为Hz)是频率的基本单位,定义为每秒一个周期。以Heindrich Rudolf Hertz的名字命名,后者为电磁波的存在提供了确凿的证据。检测到或存储的任何信号都与时间密切相关:可能可以肯定地说,任何信号都是一个 时间序列, 使用“略有不同”的工具进行分析。
提供给每个家庭的交流电(AC)是一种以50Hz或60Hz振荡的电流,音频信号通常覆盖20Hz-20,000Hz(或20kHz)之间的频谱,移动频段覆盖850-900MHz的一些窄带, 1800Mhz(1.8GHz)和1900MHz,Wifi信号以2.4 GHz或5 GHz附近的一些预定义频率振荡。这些只是围绕信号的一些示例。听说过射电望远镜吗?威尔金森微波各向异性探测器能够扫描夜空和检测信号围绕着5高频带的:23千兆赫,33千兆赫,41千兆赫,61千兆赫和94千兆赫,从而帮助了解宇宙的开端。但是,仍处于电磁波频谱的中间。
Figure 1: Source: Electromagnetic spectrum, https://www.nasa.gov/directorates/heo/scan/spectrum/txt_electromagnetic_spectrum.html
Digital or analog
In the early 20th century, almost all signals we dealt with were analog. Amplifying or recording speech or music was done on tapes and through fully analog signal paths using vacuum tubes, transistors, or, nowadays, operational amplifiers. However, the storage and reproduction of signals (music or else) have changed with the advent of Digital Signal Processing (or DSP). Still, remember CDs? Even if not, the music today is stored as a string of zeros and ones. However, when you play a song, the signal that drives the speaker is analog. In order to play an MP3, the signal needs to be converted from digital to analog and this can be achieved by passing it through the Digital-to-Analog converter (DAC): then the signal can be amplified and played through the speaker. The reverse process happens when you want to save the signal in a digital format: an analog signal is passed through an Analog-to-Digital converter (ADC) that digitizes the signal.
With the emergence of the high-speed Internet and 5th Generation mobile networks, signal analysis and processing has become a vital tool in many domains. cuSignal brings the processing power of NVIDIA GPUs into this domain to help with the current and emerging demands of the field.
在20世纪初,处理几乎所有信号都是模拟信号。使用磁带,晶体管或当今的运算放大器,在磁带上通过完全模拟的信号路径来放大或记录语音或音乐。但是,随着数字信号处理(或DSP)的出现,信号(音乐或其它)的存储和再现已发生了变化。还是记得CD吗?即使不是,今天的音乐也存储为零和一串。但是,当播放歌曲时,驱动扬声器的信号是模拟信号。为了播放MP3,需要将信号从数字转换为模拟,可以将其通过数模转换器(DAC)来实现:然后可以将信号放大并通过扬声器播放。当以数字格式保存信号时,将发生相反的过程:数字化信号。
随着高速互联网和第五代移动网络的出现,信号分析和处理已成为许多领域的重要工具。cuSignal将NVIDIA GPU的处理能力带入了这一领域,以帮助满足该领域的当前和新兴需求。
Convolution
One of the most fundamental tools to analyze signals and extract meaningful information is convolution. Convolution is a mathematical operation that takes two signals and produces a third one, filtered. In the signal processing domain, convolution can be used to filter some frequencies from the spectrum of the signal to better isolate or detect some interesting properties. Just like in Convolutional Neural Networks, where the network learns different kernels to sharpen, blur or otherwise extract interesting features from an image to, for example, detect objects, the signal convolutions use different windows that help to refine the signal.
Let’s assume that we have a digital signal that looks as below.
卷积是分析信号和提取有意义的信息的最基本工具之一。卷积是一种数学运算,需要两个信号并产生第三个信号(经过滤波)。在信号处理领域,可以使用卷积从信号频谱中滤除某些频率,以更好地隔离或检测一些有趣的属性。就像在卷积神经网络中一样,该网络学习不同的内核以锐化,模糊或以其它方式从图像中提取有趣的特征以(例如)检测对象,信号卷积使用不同的窗口来帮助改善信号。
假设有一个如下所示的数字信号。
Figure 2: Sample signal with an exponentially decaying noise component.
The signal above is a 2 Vrms (Root Mean Squared) a sine wave with its frequency slowly modulated around 3kHz, corrupted by the white noise of exponentially decreasing magnitude sampled at 10 kHz. To see the effect different windows would have on this signal, we will use Hamming and Dolph-Chebyshev windows.
上面的信号是一个2 Vrms(均方根)正弦波,其频率在3kHz左右缓慢调制,并被在10 kHz采样的幅度呈指数下降的白噪声破坏。为了查看不同窗口对信号的影响,将使用 Hamming 和 Dolph-Chebyshev 窗口。
window_hamming = cusignal.hamming(51)
window_chebwin = cusignal.chebwin(51, at=100)
filtered_hamming = cusignal.convolve(
data
, window_hamming
, method='direct'
) / cp.sum(window_hamming)
filtered_chebwin = cusignal.convolve(
data
, window_chebwin
, method='direct'
) / cp.sum(window_chebwin)
Figure 3: Effects of applying Hamming and Dolph-Chebyshev windows on the original signal.
On the right, you can see the difference between the two windows. They are of similar shape but the Dolph-Chebyshev window is narrower and is effectively a more narrow band-pass filter compared to the Hamming window. Both of these methods can definitely help to find the fundamental frequency in the data.
For a full list of all the windows supported in cuSignal, refer to the cheat sheet you can download cuSignal cheatsheet, or try any of them in an interactive cuSignal notebook here.
在右侧,可以看到两个窗口之间的区别。具有相似的形状,但是与汉明窗相比,Dolph-Chebyshev窗更窄,并且实际上是更窄的带通滤波器。这两种方法绝对可以帮助找到数据中的基本频率。
有关cuSignal支持的所有窗口的完整列表,请参考备忘单,可以下载cuSignal备忘单,或在交互式cuSignal笔记本中 尝试其中的任何一种。
Spectral analysis
While filtering the signal using convolution might help to find the fundamental frequency of 3KHz, it does not show if (and how) that frequency might change over time. However, spectral analysis should allow us to do just that.
尽管使用卷积对信号进行滤波可能有助于找到3KHz的基本频率,但并未显示该频率是否(以及如何)随时间变化。但是,频谱分析应该允许做到这一点。
f, t, Sxx = cusignal.spectrogram(x, fs)
plt.pcolormesh(
cp.asnumpy(t),
cp.asnumpy(f),
cp.asnumpy(Sxx)
)
plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]')
The above code produces the following chart:
Figure 4: Spectrogram of slowly 3Hz signal with a compound 0.25Hz oscillation.
We can now clearly see not only the fundamental frequency of 3kHz is slowly, at 0.25Hz, modulated slightly over time, but we can also observe the initial influence of the white noise shown as lighter blue dots.
With the introduction of cuSignal, the RAPIDS ecosystem gained another great package with a vast array of signal processing tools that can be applied in many domains. You can try the above examples and more for yourself at app.blazingsql.com, and download the cuSignal cheat sheet here!
现在可以清楚地看到,不仅3kHz的基频在0.25Hz处缓慢变化,而且随着时间的流逝略有调制,还可以观察到白噪声的初始影响,显示为较亮的蓝点。
随着cuSignal的引入,RAPIDS生态系统获得了又一个伟大的软件包,具有可应用于许多领域的大量信号处理工具。可以在app.blazingsql.com上尝试上述示例以及更多示例,并下载cuSignal备忘单!