Ever yelled at your gadgets and wondered how they actually hear you? Spoiler: It’s microphone magic! These tiny marvels are opening doors to a world of interactive, voice-powered creations, sparking endless ideas in the maker community. Say hello to the PDM mic: compact, efficient, and perfect for capturing audio in innovative projects. But here’s the catch—working with raw audio signals can feel like deciphering an alien language. Don’t sweat it! With MicroPython on PSOC™ 6, we have made handling PDM microphones a breeze. You can now seamlessly transform audio into signals for your next-gen inventions, no complex coding required. Ready to bring your projects from silence to life?
What is PDM and PCM?PDM (Pulse Density Modulation) and PCM (Pulse Code Modulation) are two digital audio encoding techniques that are widely used in modern microphones. These methods take analog audio signals and convert them into digital formats, enabling efficient processing and transmission.
PDM (Pulse Density Modulation):PDM is a modulation technique where the density of the pulses in the signal is varied according to the analog input signal. In a PDM digital microphone, the output is a stream of single-bit digital values representing the analog audio signal.PDM microphones directly convert sound waves into a stream of high-frequency pulses, where the density of the pulses represents the audio signal. For example, a single period of the trigonometric sine function, sampled 100 times and represented as a PDM bitstream, is:
0101011011110111111111111111111111011111101101101010100100100000010000000000000000000001000010010101
PCM, on the other hand, is a method used to digitally represent analog signals where the amplitude of the analog signal is sampled at uniform intervals, and each sample is quantized to a digital code using a specific bit depth. This method provides high accuracy and is the standard for most digital audio applications. For example, in the below diagram, a sine wave (red curve) is sampled and quantized for PCM. The sine wave is sampled at regular intervals, shown as vertical lines. For each sample, one of the available values (on the y-axis) is chosen. This produces a fully discrete representation of the input signal (blue points) that can be easily encoded as digital data for storage or manipulation.
While PDM is commonly used in compact and energy-efficient microphones, PCM is typically the final format after processing, used by audio playback or recording devices. Together, they form a bridge from capturing sound in the real world to digital manipulation in your projects.
The on-board integrated microphone has a single-bit PDM output. This allows you to convert any audio captured by the microphone into a digital signal (PDM). The PDM_PCM convertor block in PSOC6 then converts this digital signal to a quantized PCM output. At physical level, the bus has 2 lines:
- CLK: This OUT signal provides the clock for the data reception. It synchronizes the timing of the data received.
- DATA : Actual PDM data INPUT.
In this tutorial, we are utilizing MicroPython on the PSOC™ 6 microcontroller, for which numerous examples encompassing ADC, GPIOs, I2C, Wi-Fi connectivity, and CO2 sensors are readily available. For further exploration, the resources can be accessed at https://www.hackster.io/Infineon_Team/projects. Feel free to delve into these valuable insights!
Hardware SetupAs such, all you need is theCY8CKIT-062S2-AI kit. The kit has two IM72D128 digital microphones integrated. The pins connected to the PDM-PCM bus in this kit are:
- CLK : P10_4
- DATA: P10_5
Get ready for the exciting part! With MicroPython, setup is a breeze thanks to the step-by-step guide. For programming, we are using Thonny IDE and to download just follow the link and dive into coding in no time!
The application here shows a simple way of recording music or any other audio and saving it into a.wav file on the device's file system. Let's go step-by-step through the code:
1. Import the necessary modules:
from machine import PDM_PCM, Pin, freq, AUDIO_PDM_24_576_000_HZ
import time
import struct
import uio
From the "machine
" library, we're importing "Pin
" for GPIO control, "PDM_PCM
" for digital audio communication, "AUDIO_PDM_24_576_000_HZ
" for setting the PDM_PCM
clock frequency to 24.576 MHz, and "freq
" for frequency management and clock control.
2. Assign the pins and create a buffer to store the real-time PCM raw values:
clk_pin = "P10_4"
data_pin = "P10_5”
rx_buf = bytearray([0] * 128)
3. Define a function that will create a file in your device's filesystem and start recording the audio for the specified time period:
def record_audio(filename, time_sec):
with uio.open(filename, "wb") as f:
f.write(wav_header)
start_time = time.time()
# Start conversion
pdm_pcm.init()
while time.time() - start_time < time_sec: # record for "time_sec" second
num_read = pdm_pcm.readinto(rx_buf)
f.write(rx_buf)
The init()
function is responsible to start the hardware conversion block and starts recording operation while the readinto() copies the converted raw data into the provided buffer.
4. Before initializing the microphone object, the clock needs to be set accordingly. For that you can use the freq module. Please note that different sample rates might require different clock settings. You can find more details on this here. Now we are configuring the bus to sample audio at 8 kHz:
freq(AUDIO_PDM_24_576_000_HZ)
5. Initialize PDM_PCM
instance and get the interface ready:
pdm_pcm = PDM_PCM(
0,
sck=clk_pin,
data=data_pin,
sample_rate=8000,
decimation_rate=64,
bits=PDM_PCM.BITS_16,
format=PDM_PCM.MONO_LEFT,
left_gain=0,
right_gain=0,
)
The code initializes an PDM_PCM
instance for audio input with specific parameters such as pin assignments, mode, sample size, format, sample rate, and gain, preparing the interface to receive audio data based on the defined settings from before.
6. Create a.wav file with the required header:
wav_header = bytearray(44) # 44-byte WAV header
struct.pack_into('<4sI4s4sIHHIIHH', wav_header, 0,
'RIFF', # ChunkID
36 + 80 * 32, # ChunkSize (approximate)
'WAVE', # Format
'fmt ', # Subchunk1ID
16, # Subchunk1Size
1, # AudioFormat (PCM)
1, # NumChannels (MONO_LEFT)
8000, # SampleRate
8000 * 2, # ByteRate
2, # BlockAlign
16, # BitsPerSample
)
struct.pack_into('<4sI', wav_header, 36,
'data', # Subchunk2ID
80 * 32) # Subchunk2Size (approximate)
Please be aware that the field in the header is configured for values utilized in configuring the PDM_PCM
interface. If you have a different sampling rate, mode, or number of channels, it is essential to adjust the values accordingly. Further information regarding the fields in the.wav header can be found here.
7. Finally let’s call the function record_audio
to start with recording:
record_audio("pcm_audio.wav", 30)
Now make some noise or play some music :)
As soon as the passed time period of 30s ended, refresh the device filesystem from the Thonny IDE
and you will find the.wav saved in your device as shown below:
8. Now, you can bring this.wav file to your local machine and import it to the audio processing tool of your choice:
9. We use Audacity, a powerful and free GUI tool for audio processing. To import your.wav file, open the Audacity tool and click on: File -> Import -> Raw Data… and ensure the import configuration matches your record settings:
10. Finally, the audio track will pop up in your project and you can play back the audio:
Recorded the audio either too loud or too feeble? Use normalization to set the peak amplitude of the audio track or to remove any DC offset from your track. Normalization in audio context is simply either amplifying or attenuating to ensure that a given audio track uses the full volume spectrum without clipping. Below is the audio snapshot showing the same recording after normalization:
Here you find all details on normalization using Audacity.
For improved audio results, one can also explore varying sampling rates, modifying modes, and utilizing digital signal processing methods. Delving into advanced audio processing algorithms, like noise reduction and signal filtering, can notably enhance recorded audio quality. Currently the recoding is done in blocking mode. And if interested, you could also record in non-blocking mode, the details of which are provided here.
Got Problems with MicroPython for PSOC™ 6?
Leave us a note here and we will do all it takes to fix! 🚀
Curious for More?Find additional information related to the PSOC™ 6 MicroPython enablement in our documentation or open a topic in our discussion section on GitHub.
Comments