复制微信号并打开微信_理解信号并不那么复杂

复制微信号并打开微信

Signal processing might seem like something impenetrably complicated, even to scientists. Even without fully aware of its underlying presence, signal processing is at the heart of our everyday life. This article will explore what a signal is, how we can generate, and store signals in Numpy for machine learning.

信号处理看起来似乎非常复杂,即使对于科学家而言。 即使没有完全意识到其潜在的存在,信号处理仍是我们日常生活的核心。 本文将探讨什么是信号,如何生成信号并将信号存储在Numpy中以进行机器学习。

Sound is a wave that results from the back and forth vibration of the medium particles through which the sound wave moves. These sound waves consist of a repeating pattern of high-pressure and low-pressure regions. They are also referred to as pressure waves.

声波是由声波在其中传播的介质粒子的来回振动产生的波。 这些声波由高压和低压区域的重复模式组成。 它们也称为压力波。

Image for post
An example of a sine wave.
正弦波的一个例子。

When we hear something, our brain translates these air molecules’ movements that our ears pick up, into something we can recognize. Be it words or music, birds chipping or a car horn.

当我们听到某些声音时,我们的大脑会将耳朵吸收的这些空气分子的运动转化为可以识别的东西。 无论是文字还是音乐,鸟鸣或汽车喇叭。

Usually, when we describe sounds, we refer to them in terms of their frequency. We measure these waves by the number of cycles in a second, in a hertz (Hz) measurement unit.

通常,当我们描述声音时,我们以声音的频率来指代它们。 我们通过以赫兹(Hz)为单位的每秒循环数来测量这些波。

For example, this is a 440 Hz sound wave, the sound of the A note above the middle C on the piano:

例如,这是一个440 Hz声波,钢琴中间C上方的A音符的声音:

440 Hz audio tone by MediaCollege (right click and open in a new tab)

MediaCollege提供的440 Hz音频音调(右键单击并在新选项卡中打开)

If we were to measure this sound and convert it into a digital signal in a function of time. This is the graph we will get, a 440 Hz sine wave. It oscillates up and down at the equilibrium, at 440 oscillations every second.

如果我们要测量这种声音并将其转换为时间函数的数字信号。 这是我们将得到的440 Hz正弦波图。 它在平衡下上下振荡,每秒440次振荡。

Image for post
A 440 Hz in 1/10 of a second.
1/10秒内为440 Hz。

Let’s take another sound wave, 250 Hz, which sounds like this:

让我们再听一个250 Hz的声波,听起来像这样:

250 Hz audio tone by MediaCollege (right click and open in a new tab)

MediaCollege提供的250 Hz音频(右键单击并在新选项卡中打开)

A 250 Hz tone has a lower pitch than 440 Hz. If we were to plot it, It will look similar to the 440 Hz wave but with a lower frequency. It oscillates at 250 oscillations per second.

250 Hz音调的音调低于440 Hz。 如果我们将其绘制出来,它将看起来类似于440 Hz波,但频率更低。 它以每秒250次振荡的速度振荡。

Image for post
A 250 Hz in 1/10 of a second.
1/10秒内为250 Hz。

A single sound wave alone isn’t exciting. But when we combine a few frequencies and play them in the right sequence, we can produce music. We make these sound waves with our vocal cord to speak words that we use for communication.

单独一个声波并不令人兴奋。 但是,当我们组合几个频率并以正确的顺序播放它们时,我们就可以产生音乐。 我们用声带发出这些声波,以说出用于交流的单词。

When we combine two frequencies and play them together, the new sound wave is the sum of the two sound waves. What we get is a new wave that is not a pure sine wave. At some points, the peaks add up to become higher, while other points cancel out each other resulting in zero.

当我们组合两个频率并将它们一起播放时,新的声波就是两个声波的总和。 我们得到的是不是纯正弦波的新波。 在某些点,峰值加起来变得更高,而其他点互相抵消,导致零。

Image for post
Combining 2 sine waves results in a new wave.
合并两个正弦波会产生一个新波。

We can add more sound frequencies; the sound wave will get more and more complicated. In fact, these complicated sound waves are what our microphones pick up. Our microphones pick up many different frequencies at any point in time; the final recordings are the sum of all the sound frequencies combined. Other than those sound that you actually want to record, it also picks up background noise, echos, and even some electric signal noise.

我们可以增加更多的声音频率; 声波将变得越来越复杂。 实际上,这些复杂的声波正是我们的麦克风拾取的。 我们的麦克风在任何时间点都会拾取许多不同的频率。 最终录音是所有声音频率之和。 除了您实际想要录制的声音之外,它还会拾取背景噪声,回声,甚至还有一些电信号噪声。

Image for post
The wave gets more complicated as more frequencies are added.
随着添加更多频率,电波变得更加复杂。

Even though these examples are on sound waves, these concepts are also applied in other digital signals such as an electrocardiogram and electroencephalogram. See how we use signals in brain-computer interface applications:

即使这些示例在声波上,这些概念也适用于其他数字信号,例如心电图脑电图。 了解我们如何在脑机接口应用程序中使用信号:

那么我们如何数字存储信号呢? (So how do we store signals digitally?)

As analog signals are continuous in both time and amplitude, we need to reduce the signal into a discrete-time signal, both in time and amplitude. This signal reduction process is call sampling. The sampling rate defines the number of data points in one second, literally how fast samples are taken.

由于模拟信号在时间和幅度上都是连续的,因此我们需要在时间和幅度上将信号减小为离散时间信号。 这个信号减少过程就是呼叫采样。 采样率定义了一秒内的数据点数量,即从多大程度上采样。

The sampling rate determines the signal’s fidelity. There is a minimum sampling rate for each signal to preserve the information contained in the signal. According to the Nyquist–Shannon sampling theorem, the sampling rate must be at least twice the signal’s maximum frequency to allow the signal to be completely represented. This means that if the signal contains high-frequency components, we will need to sample at a higher rate.

采样率决定了信号的保真度。 每个信号都有一个最小采样率,以保留信号中包含的信息。 根据Nyquist–Shannon采样定理,采样率必须至少是信号最大频率的两倍,才能完全表示信号。 这意味着,如果信号包含高频分量,我们将需要以更高的速率采样。

In theory, as long as the Nyquist limit, which is half the sample rate, exceeds the highest frequency of the signal being sampled, the original analog signal can be reconstructed without loss. Otherwise, the signal information may not be completely represented, where some of the original signal frequencies may be lost. It will result in audible artifacts known as “aliasing,” which are unwanted components in the reconstructed signal.

从理论上讲,只要是采样率的一半的奈奎斯特极限超过被采样信号的最高频率,就可以重建原始模拟信号而不会造成损失。 否则,可能无法完全表示信号信息,从而可能丢失某些原始信号频率。 这将导致被称为“混叠”的可听伪像,它们是重构信号中不需要的成分。

Let’s take a look at what happens when the sampling rate gets too low.

让我们看一下采样率太低时会发生什么。

Image for post
2 Hz signals lasting 2-seconds, with various sampling rate (SR)
2 Hz信号持续2秒,具有各种采样率(SR)

The figure above shows a 2 Hz signal lasting 2-seconds; it has four oscillations in total, two oscillations per second. At a sampling rate of 50 and 100, the wave looks excellent as there are enough data points. We start to see edges in the signal at 10 and 20 sampling rates though we can still clearly see four positive peaks. The signal begins to lose some information when the sampling rate falls below 6.

上图显示了持续2秒的2 Hz信号; 它总共有四个振荡,每秒两个振荡。 在50和100的采样率下,由于有足够的数据点,该波形看起来非常好。 尽管我们仍然可以清楚地看到四个正峰值,但我们开始以10和20的采样率看到信号的边缘。 当采样率降至6以下时,信号开始丢失一些信息。

As humans can hear 20–20,000 Hz range; this is why audio waveform and early professional audio equipment manufacturers chose sampling rates at 44.1kHz, the “standard” sampling rate for many digital formats. Increasing the sampling rate generally improves the sound quality, but it also increases the disk space required to store. Many telephone and walkie-talkie signals are transmitted on 8,000 Hz to reduce packet size to improve transmission.

人类可以听到20–20,000 Hz的范围; 这就是音频波形和早期专业音频设备制造商选择44.1kHz采样率的原因,这是许多数字格式的“标准”采样率。 提高采样率通常可以改善声音质量,但同时也增加了存储所需的磁盘空间。 许多电话和对讲机信号以8,000 Hz传输,以减小数据包大小以改善传输。

For machine learning, sampling at higher frequencies results in better-reconstructed signals, resulting in better performance. However, this requires faster CPU/GPU to convert and process the signals, and a bigger GPU memory is needed as it also increases the model’s size as your inputs get larger. Therefore, we must weigh each application’s advantages and disadvantages and be aware of the tradeoffs involved.

对于机器学习,以较高的频率采样会产生更好的重构信号,从而产生更好的性能。 但是,这需要更快的CPU / GPU来转换和处理信号,并且需要更大的GPU内存,因为随着输入的增加,这也会增加模型的大小。 因此,我们必须权衡每个应用程序的优缺点,并要意识到所涉及的折衷。

So far, we have seen signals with a single channel. But signals in real-world applications can have multiple channels. For example, we have two channels in audio, left and right channels. In electroencephalography and electrocardiography, we can have ten or more channels. Neuralink aims to build an integrated brain-machine interface platform with thousands of channels.

到目前为止,我们已经看到了具有单个通道的信号。 但是实际应用中的信号可以具有多个通道。 例如,我们在音频中有两个声道,左声道和右声道。 在脑电图和心电图中,我们可以有十个或更多的通道。 Neuralink旨在构建具有数千个通道的集成式脑机接口平台。

生成一些信号(在Python中) (Generate some signals (in Python))

We can generate signals with three parameters, 1) signal duration, sampling rate, and frequencies.

我们可以生成具有三个参数的信号:1)信号持续时间,采样率和频率。

As we are storing the signals as a sequence of numbers, first, we need the number of data points of the signal. This can be done by multiplying the signal duration with the sampling rate. Next, we need a time variable, a function over time that allows us to generate the waveform for each data point.

当我们将信号存储为数字序列时,首先,我们需要信号的数据点数量。 这可以通过将信号持续时间乘以采样率来完成。 接下来,我们需要一个时间变量,一个随时间变化的函数,使我们能够为每个数据点生成波形。

We can either generate a sine or a cosine wave with a sine/cosine periodic function. The sine function tracks the y-coordinates of a point traveling around the unit circle while the cosine function follows the x-coordinates.

我们可以生成具有正弦/余弦周期函数的正弦波或余弦波。 正弦函数跟踪在单位圆周围移动的点的y坐标,而余弦函数遵循x坐标。

Image for post
Wikipedia] 维基百科]

Using either function will generate a similar but different wave.

使用这两种功能都会产生相似但不同的波形。

Image for post
A sine and a cosine wave.
正弦和余弦波。

To create a more realistic signal, we can also add noise to the generated signal by adding random values to each data point. This allows us to test our model ability to generalize and learn from a noisy dataset.

为了创建更真实的信号,我们还可以通过将随机值添加到每个数据点来将噪声添加到生成的信号中。 这使我们能够测试模型的泛化能力并从嘈杂的数据集中学习。

Image for post
A 2 Hz signals with various noise levels.
具有各种噪声水平的2 Hz信号。

Recapping from the previous sections, the value of a point is the sum of all the frequencies. If we want to generate a signal that contains more than one frequencies, we can simply sum each generated waveform. Here are some examples of generated signals; each wave adds additional frequencies to the previous:

从前面的部分重新开始,点的值是所有频率的总和。 如果我们要生成一个包含多个频率的信号,我们可以简单地对每个生成的波形求和。 这里是一些产生信号的例子。 每波都为前一个波增加频率:

Image for post
Adding different frequencies to a waveform.
向波形添加不同的频率。

Lastly, an example of a multiple channel signal:

最后,一个多通道信号示例:

Image for post
Adding different waveforms to multiple channels.
将不同的波形添加到多个通道。

Here are the codes from the torchsignal package if you plan to generate some signals.

如果您打算生成一些信号,则以下是火炬信号包中的代码。

import numpy as np
import matplotlib.pyplot as plt




def generate_signal(length_seconds, sampling_rate, frequencies_list, func="sin", add_noise=0, plot=True):
    r"""
    Generate a `length_seconds` seconds signal at `sampling_rate` sampling rate. See torchsignal (https://github.com/jinglescode/torchsignal) for more info.
    
    Args:
        length_seconds : int
            Duration of signal in seconds (i.e. `10` for a 10-seconds signal)
        sampling_rate : int
            The sampling rate of the signal.
        frequencies_list : 1 or 2 dimension python list a floats
            An array of floats, where each float is the desired frequencies to generate (i.e. [5, 12, 15] to generate a signal containing a 5-Hz, 12-Hz and 15-Hz)
            2 dimension python list, i.e. [[5, 12, 15],[1]], to generate a signal with 2 signals, where the second channel containing 1-Hz signal
        func : string, default: sin
            The periodic function to generate signal, either `sin` or `cos`
        add_noise : float, default: 0
            Add random noise to the signal, where `0` has no noise
        plot : boolean
            Plot the generated signal
    Returns:
        signal : 1d ndarray
            Generated signal, a numpy array of length `sampling_rate*length_seconds`
    """
    
    frequencies_list = np.array(frequencies_list, dtype=object)
    assert len(frequencies_list.shape) == 1 or len(frequencies_list.shape) == 2, "frequencies_list must be 1d or 2d python list"
    
    expanded = False
    if isinstance(frequencies_list[0], int):
        frequencies_list = np.expand_dims(frequencies_list, axis=0)
        expanded = True
        
    npnts = sampling_rate*length_seconds  # number of time samples
    time = np.arange(0, npnts)/sampling_rate
    signal = np.zeros((frequencies_list.shape[0],npnts))
    
    for channel in range(0,frequencies_list.shape[0]):
        for fi in frequencies_list[channel]:
            if func == "cos":
                signal[channel] = signal[channel] + np.cos(2*np.pi*fi*time)
            else:
                signal[channel] = signal[channel] + np.sin(2*np.pi*fi*time)
    
        # normalize
        max = np.repeat(signal[channel].max()[np.newaxis], npnts)
        min = np.repeat(signal[channel].min()[np.newaxis], npnts)
        signal[channel] = (2*(signal[channel]-min)/(max-min))-1
    
    if add_noise:        
        noise = np.random.uniform(low=0, high=add_noise, size=(frequencies_list.shape[0],npnts))
        signal = signal + noise


    if plot:
        plt.plot(time, signal.T)
        plt.show()
    
    if expanded:
        signal = signal[0]
        
    return signal

With this code, you could generate your own datasets, where the input features are the raw signals (with noise), and the predicted outputs are the frequencies.

使用此代码,您可以生成自己的数据集,其中输入特征是原始信号(带有噪声),而预测输出是频率。

As I am currently working on my Ph.D. on brain-computer interface research, I’ve made a repo containing codes commonly used in signal processing. This package includes functions to clean signals and other signal processing techniques. Feel free to check it out and star this repo, torchsignal.

由于我目前正在攻读博士学位。 在脑机接口研究中,我制作了一个包含信号处理中常用代码的存储库。 该软件包包括清除信号和其他信号处理技术的功能。 随时检查它,并为这个仓库,火炬信号加注星标。

Seeking for collaborators to contribute new features, utility functions, bug fixes, and documentation. Currently, I am working on this alone. If you are working on signal processing or brain-computer interface, and keen to build a high-quality package to apply PyTorch to the signal processing domain, do reach out to me.

寻求合作者贡献新功能,实用程序功能,错误修复和文档。 目前,我正在独自处理。 如果您正在研究信号处理或人机界面,并且希望构建高质量的程序包以将PyTorch应用于信号处理领域,请与我联系

Generating signals is useful for creating ideal datasets to test models’ performance in a “lab environment.” In the next article, I will introduce how we can filter and clean signals for machine learning models.

生成信号对于创建理想的数据集以在“实验室环境”中测试模型的性能很有用。 在下一篇文章中,我将介绍如何过滤和清除机器学习模型的信号。

翻译自: https://towardsdatascience.com/understanding-signals-its-not-that-complicated-7f2b157da9c7

复制微信号并打开微信

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值