Saturday, July 11, 2020

Characterizing the Audio Signal: Transforming to Frequency Domain

CoBlog - The Cobalt Blog — Cobalt

Characterizing an audio signal involves converting the time domain signal into frequency domain, and understanding its frequency components, by. This is an important step because it gives a lot of information about the signal. You can use a mathematical tool like Fourier Transform to perform this transformation.

Example

The following example shows, step-by-step, how to characterize the signal, using Python, which is stored in a file. Note that here we are using Fourier Transform mathematical tool to convert it into frequency domain.

Import the necessary packages, as shown here:

import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile


Now, read the stored audio file. It will return two values: the sampling frequency and the the audio signal. Provide the path of the audio file where it is stored as shown in the command here:

frequency_sampling, audio_signal = wavfile.read("/Users/admin/sample.wav")

In this step, we will display the parameters like sampling frequency of the audio signal, data type of signal and its duration, using the commands given below:

print('\nSignal shape:', audio_signal.shape)
print('Signal Datatype:', audio_signal.dtype)
print('Signal duration:', round(audio_signal.shape[0] / float(frequency_sampling), 2), 'seconds')

In this step, we need to normalize the signal, as shown in the following command:

audio_signal = audio_signal / np.power(2, 15)

This step involves extracting the length and half length of the signal. Use the following commands for this purpose:

length_signal = len(audio_signal)
half_length = np.ceil((length_signal + 1) / 2.0).astype(np.int)


Now, we need to apply mathematics tools for transforming into frequency domain. Here we are using the Fourier Transform.

signal_frequency = np.fft.fft(audio_signal)

Now, do the normalization of frequency domain signal and square it:

signal_frequency = abs(signal_frequency[0:half_length]) / length_signal
signal_frequency **= 2


Next, extract the length and half length of the frequency transformed signal:

len_fts = len(signal_frequency)

Note that the Fourier transformed signal must be adjusted for even as well as odd case.
if length_signal % 2:

signal_frequency[1:len_fts] *= 2
else:
signal_frequency[1:len_fts-1] *= 2


Now, extract the power in decibal(dB):

signal_power = 10 * np.log10(signal_frequency)

Adjust the frequency in kHz for X-axis:

x_axis = np.arange(0, len_half, 1) * (frequency_sampling / length_signal) / 1000.0

Now, visualize the characterization of signal as follows:

plt.figure()
plt.plot(x_axis, signal_power, color='black')
plt.xlabel('Frequency (kHz)')
plt.ylabel('Signal power (dB)')
plt.show()


You can observe the output graph of the above code as shown in the image below:


In the next post we are going to generate a monotone signal using a Python program.
Share:

0 comments:

Post a Comment