这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版 | ||
knowledge:electronic:2019032101 [2023/07/06 08:05] – [基于MAX4466的模块] ob | knowledge:electronic:2019032101 [2023/07/07 05:32] (当前版本) – [Summary] ob | ||
---|---|---|---|
行 26: | 行 26: | ||
{{: | {{: | ||
- | I got mine from the Far East but it looks like it is based on an Adafuit design. This module has an adjustable gain that you control with a tiny 1-turn potentiometer. There is a Vcc pin, a ground pin and an analog out pin. The analog pin emits a waveform where “0” is Vcc/2 and the amplitude depends on the gain and the volume of the sound. The MAX4466 chip is an op amp that is specifically optimized for use as a microphone amplifier, which makes this module a great performer and my eventual choice for the project. | + | 我从Far East买了这个模块,但看起来它是基于 Adafuit 设计的。该模块具有可调节增益,您可以使用微型单匝电势计进行控制。有一个 Vcc 引脚、一个接地引脚和一个模拟输出引脚。模拟引脚发出波形,其中“0”为 Vcc/ |
- | 我从Far East买了这个,但看起来它是基于 Adafuit 设计的。该模块具有可调节增益,您可以使用微型单匝电势计进行控制。有一个 Vcc 引脚、一个接地引脚和一个模拟输出引脚。模拟引脚发出波形,其中“0”为 Vcc/ | + | |
- | ===== “HXJ-17” / “Keyes” | + | ===== 基于 LM393 的“HXJ-17”/ |
{{: | {{: | ||
- | I got this module from a local electronics store. Not sure who designed it, but it has a multi-turn potentiometer, | + | 我从当地的一家电子商店购买了这个模块。不知道是谁设计的,但它有一个多圈电位器,没有放大器和一个LM393比较器芯片。有一个 |
- | ===== “Sound detection” module based on an LM393 ===== | + | ===== 基于LM393的“声音检测”模块 |
{{: | {{: | ||
- | I got this one from the same local shop. This one is similar to the HXJ-17, but it is even simpler. It has a one turn potentiometer and no analog output. Leaving this useful for knowing if there is or isn’t a loud sound. | + | 我从同一家当地商店买了这个。这一款与HXJ-17类似,但更简单。它有一个一圈电位器,没有模拟输出。让它有助于了解是否有响亮的声音。 |
- | ===== Analyzing analog input ===== | + | ===== 分析模拟输入 |
- | As the first step, I would suggest you take some time to analyze the analog output of your module to see the baseline and amplitude. I have used the following | + | 作为第一步,我建议您花一些时间分析模块的模拟输出,以查看基线和幅度。我使用以下 |
< | < | ||
行 75: | 行 74: | ||
</ | </ | ||
- | You can then make some sounds at different volume levels and see how your average, min, max and span values respond. Looking at the result you might see that you need to adjust the gain potentiometer such that you utilize the max span for your sound levels while not overdoing it so not to clip your signal. | + | 然后,您可以以不同的音量发出一些声音,并查看平均值、最小值、最大值和跨度值的响应情况。查看结果,您可能会发现需要调整增益电位器,以便利用声级的最大跨度,同时不要过度使用,以免削波信号。 |
+ | ===== 使用 3.3V 参考电压和自由运行实现精确采样 ===== | ||
+ | Arduino 的 AnalogRead 功能可以轻松获取模拟引脚的数字值。它是在考虑单个样本收集的情况下实施的。在对声音进行采样时,以恒定的速率采样并准确地采样每个样本非常重要。为了实现这两个属性,我们进行以下操作。 | ||
- | ===== Implementing accurate sampling with 3.3V reference and free running ===== | + | 首先,我们将配置 |
- | + | 使用以下代码来配置此模式: | |
- | The analogRead function of Arduino makes it simple to get a digital value of an analog pin. It was implemented with single sample collection in mind. When sampling sound it is important to take our samples at a constant rate and to take each sample accurately. To achieve these two properties we will change a couple of things. | + | |
- | + | ||
- | First, we will configure the ADC to use 3.3V as the analog reference voltage. The reason for this is that the 3.3V is usually more stable than the 5V. The 5V can fluctuate up and down especially when the Arduino | + | |
- | + | ||
- | Use the following code to configure this mode: | + | |
< | < | ||
行 89: | 行 85: | ||
</ | </ | ||
- | Second, we will configure the ADC to work in “free-running” mode and read the sample values directly from internal registers, bypassing | + | 其次,我们将配置 |
- | Setup “free-running” mode with this code: | + | 使用以下代码设置“自由运行”模式: |
< | < | ||
行 104: | 行 100: | ||
</ | </ | ||
- | Read a batch of samples with this code: | + | 使用以下代码读取一批示例: |
< | < | ||
行 118: | 行 114: | ||
</ | </ | ||
- | Third, you can also adjust the speed of the ADC. By default the ADC is running at 1:128 of the MCU speed (mode #7). Each sample takes the ADC about 13 clock cycles to get processed. So by default we get 16Mhz/ | + | 第三,还可以调整ADC的速度。默认情况下,ADC 以 MCU 速度的 |
- | Here is an example of how to set divider to 32 (mode #5) which equals a sampling rate of 16Mhz/ | + | 以下是如何将分频器设置为 |
< | < | ||
行 134: | 行 130: | ||
</ | </ | ||
- | You can see all three of these code snippets implemented together in the source code of the project at github. | + | 您可以在 |
- | With this logic in place we can get decent waveform data for the Arduino | + | 有了这个逻辑,我们就可以获得合适的波形数据供 |
- | ===== Sound level measurement | + | ===== 声级测量 |
- | ==== Theory | + | ==== 理论 |
- | The sound level is defined as the amplitude of the waveform and can be measured per set of samples which represent a part of a signal. | + | 声级定义为波形的幅度,可以根据代表信号一部分的样本集进行测量。 |
{{: | {{: | ||
- | For an ideal sine signal, the amplitude would be the max sample, but in practice it is easy for some samples to be outliers and to affect the max value significantly. For this reason it is more practical to use a metric that takes all the sample values into account. You can use an average, but it is more common to use Root Mean Square (RMS) which will give more “weight” to higher values. | + | 对于一个理想的正弦信号来说,振幅应该是最大的样本值。但在实际应用中,有些样本可能成为异常值,并且会对最大值产生显著影响。因此,更实际的做法是使用一个考虑所有样本值的度量标准。你可以使用平均值,但更常见的是使用均方根(RMS),它会给较高的值赋予更多的“权重”。 |
- | The relationship between amplitude and RMS for sine waves is known and is amplitude= sqrt(2)*RMS. If we assume that a sound waveform is similar to a sine waveform we can use this relationship to estimate a stable amplitude based on an RMS value that we calculate. | + | 振幅和均方根(RMS)之间的关系是已知的,可以表示为振幅= sqrt(2)*RMS。如果我们假设声音波形与正弦波形相似,我们可以利用这个关系来基于计算得到的均方根值估计一个稳定的振幅。 |
{{: | {{: | ||
- | The values we are dealing with are relative and not absolute. After all we are using some value of gain to tune the volume levels to our digital 10bit range. It is very common to work with relative values when processing sound waveforms. The volume is then measured as a ratio from some other “reference point” value. A common unit used to express the ratio is decibel | + | 我们处理的数值是相对值,而不是绝对值。毕竟,我们使用一定的增益值来调整音量水平到我们的10位数字范围内。处理声音波形时,使用相对值是非常常见的。音量通常被测量为与其他“参考点”值的比率。用于表达该比率的常用单位是分贝 |
dB=10*log10(v/ | dB=10*log10(v/ | ||
- | Where dB is the level is dB units, | + | 其中dB为电平,单位为dB,v为样本值,v0为参考值。 |
- | Since sound pressure is a field quantity a ratio of squares is used and the value “2” | + | 由于声压是一个场量,因此使用平方比,并且对数中的值“2”变为“20”[由于 |
dB=20*log10(v/ | dB=20*log10(v/ | ||
- | I was trying to achieve relative measurements, | + | 我试图实现相对测量,我选择 |
- | When using a reference value that is higher than your samples (max amplitude), your dB values would be negative and smaller as you approach the max. | + | 当使用低于样本(感知阈值)的参考值时,您的 |
- | When using a reference value that is lower than your samples (threshold of perception), | + | 有几个因素会影响实践中的测量而使这一情况变得更加复杂。首先,人耳对所有频率的敏感度并不相同。通常会对不同频段应用不同的权重。一种这样的测量单位被称为dBA,但还有其他一些略微不同的权重。其次,您的麦克风可能不会对所有频率都具有相同的灵敏度。第三,您的扬声器可能无法以相同的精确水平再现所有频率。这些复杂性需要非常精确和昂贵的设备以及特殊的校准程序,以便能够根据标准正确测量声级。要知道,这篇文章介绍的测量声级的能力非常有限,仅适合粗略的相对测量。 |
+ | ===== 执行 ===== | ||
- | To make this even more complex, several factors affect measurement in practice. First, the human ear is not equally sensitive to all frequencies. It is typical to apply different weights to different frequency ranges. One such a unit of measurement is called dBA, but there are others with slightly different weights. Second, your microphone might not have equal sensitivity to all frequencies. Third, your speakers might not have equal ability to reproduce all frequencies at the same exact level. These complexities require very accurate and expensive equipment together with special calibration procedures to be able to measure sound levels correctly per standards. You need to understand that your ability to measure sound level with the setup described here is pretty rudimentary and suitable for rough relative measurements only. | + | 让我们回顾一下,我们的值是 |
- | + | ||
- | ===== Implementation ===== | + | |
- | + | ||
- | Let’s recap that our values are 0 to 1024 which stand for [-max, | + | |
< | < | ||
行 223: | 行 216: | ||
</ | </ | ||
- | So now with proper module and calibration you can measure sound level of different events or devices and compare them one to the other. | + | 因此,现在通过适当的模块和校准,您可以测量不同事件或设备的声级,并将它们相互比较。 |
- | ===== Frequency analysis with FHT ===== | + | ===== 使用 |
- | What if you want to “break” the sound into individual frequencies and measure or visualize each individual frequency? Can this be done with Arduino? The answer is that it can be done relatively easily thanks to some existing libraries. To turn signals from a time domain to a frequency domain you would generally use a Fourier transform. Such transforms are used for signals of different types, sound, images, radio transmissions, | + | 如果您想将声音“分解”为单独的频率并测量或可视化每个单独的频率,该怎么办?这可以用Arduino来完成吗?答案是肯定的,使用一些现有的库,这可以相对容易地完成。要将信号从时域转换到频域,通常会使用傅里叶变换。这种变换用于不同类型的信号,声音、图像、无线电传输等。每种信号类型都有其自己的属性,最适合声音信号的变换是离散哈莱特变换(DHT)。DHT 将使用形成波形的离散真实值。为了实现 |
- | The Arduino FHT library works with vectors of 16 to 256 samples. This size is denoted as N. In this project I will be using N=256 to achieve maximum resolution, but you may use smaller values if you are short on memory or processing power. | + | Arduino FHT 库可处理 |
- | First, the algorithm takes N real numbers and results in N/2 complex numbers. Then we can pass the data to another function to calculate the magnitude of the complex numbers to get N/2 bins. In the end we get N/2 bins, each covering a frequency range of sampling_rate/ | + | 首先,该算法接受N个实数,并得到N/2个复数。然后我们可以将数据传递给另一个函数,计算复数的幅度,得到N/2个频率区间。最终,我们得到N/2个频率区间,每个区间覆盖sampling_rate/ |
- | + | 因此对于N=256和采样率为38.4KHz,我们得到128个150Hz的频率区间,其中第一个区间保存了0-150Hz的幅度值,最后一个区间保存了19050-19200Hz的幅度值。现在我们可以专注于我们感兴趣的特定频率区间,通过串行连接发送所有频率区间的值,存储这些值,以某种方式显示它们等等。 | |
- | So for N=256 and sampling_rate=38.4Khz we get 128 150hz bins with the first been holding the magnitude value of 0-150hz and the last bin holding the magnitude value of 19050-19200hz. We can now focus on specific bins that interest us, send the values of all the bins over serial connection, store the values, display them in some way, etc. | + | 使用数据的有趣方法之一是使用分析器进行可视化,尤其是在故障排除和开发时。将以下 |
- | + | ||
- | One of the fun ways to use the data, especially when troubleshooting and developing is to visualize with an analyser. Load the following | + | |
{{: | {{: | ||
- | Another way to analyze the data is for the Arduino | + | 分析数据的另一种方法是 |
< | < | ||
void MeasureFHT() | void MeasureFHT() | ||
行 274: | 行 264: | ||
</ | </ | ||
- | Then you can format the data in a spreadsheet, | + | 然后,您可以将电子表格(例如 |
{{: | {{: | ||
- | ===== Summary | + | ===== 概括 |
- | My code for this project can be found at github | + | 我的这个项目的代码可以在 |
- | The Arduino | + | Arduino |
https:// | https:// | ||