Sunday, July 12, 2020

Generating Monotone Audio Signal

The two steps that you have seen till now in our previous two posts are important to learn about signals. Now, this step will be useful if you want to generate the audio signal with some predefined parameters. Note that this step will save the audio signal in an output file.

Example

In the following example, we are going to generate a monotone signal, using Python, which will be stored in a file. For this, you will have to take the following steps:

Import the necessary packages as shown:

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


Provide the file where the output file should be saved:

output_file = 'audio_signal_generated.wav'

Now, specify the parameters of your choice, as shown:

duration = 4 # in seconds
frequency_sampling = 44100 # in Hz
frequency_tone = 784
min_val = -4 * np.pi
max_val = 4 * np.pi

In this step, we can generate the audio signal, as shown:

t = np.linspace(min_val, max_val, duration * frequency_sampling)
audio_signal = np.sin(2 * np.pi * tone_freq * t)


Now, save the audio file in the output file:

write(output_file, frequency_sampling, signal_scaled)

Extract the first 100 values for our graph, as shown:

audio_signal = audio_signal[:100]
time_axis = 1000 * np.arange(0, len(signal), 1) / float(sampling_freq)


Now, visualize the generated audio signal as follows:

plt.plot(time_axis, signal, color='blue')
plt.xlabel('Time in milliseconds')
plt.ylabel('Amplitude')
plt.title('Generated audio signal')
plt.show()

You can observe the plot as shown in the figure given here:


In the next post we'll discuss about feature extraction from speech.
Share:

0 comments:

Post a Comment