Hi, I wanted to ask if there is a way to add gain to an audio signal using MATLAB? For ex., I have an audio file, and I want to add ~30dB gain to the signal. How can I do that?
Prashant Kumar answered .
2025-11-20
Sure, adding gain to an audio signal in MATLAB is a straightforward process. Here's how you can do it:
Read the Audio File: Use the audioread function to read the audio file.
Convert dB to Linear Scale: Gain in dB needs to be converted to a linear scale.
Apply the Gain: Multiply the audio signal by the gain factor.
Write the Modified Audio File: Use the audiowrite function to save the modified audio signal.
Here's a step-by-step example:
% Step 1: Read the audio file
[audioIn, fs] = audioread('your_audio_file.wav');
% Step 2: Convert dB gain to linear scale
gain_dB = 30; % Gain in dB
gain_factor = 10^(gain_dB / 20);
% Step 3: Apply the gain
audioOut = audioIn * gain_factor;
% Step 4: Write the modified audio file
audiowrite('output_audio_file.wav', audioOut, fs);
% Display message
disp('Gain applied successfully.');
Read the Audio File: audioread reads the audio file into the variable audioIn and returns the sampling frequency fs.
Convert dB to Linear Scale: The gain_factor is calculated using the formula 10^(gain_dB / 20), which converts the gain in decibels (dB) to a linear scale factor.
Apply the Gain: The audio signal is multiplied by the gain_factor to apply the gain.
Write the Modified Audio File: The modified audio signal audioOut is saved to a new file using audiowrite.
Make sure to replace 'your_audio_file.wav' with the actual name of your audio file. This script will increase the volume of the audio signal by approximately 30 dB.