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
audioreadfunction 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
audiowritefunction 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.');
Explanation:
-
Read the Audio File:
audioreadreads the audio file into the variableaudioInand returns the sampling frequencyfs. -
Convert dB to Linear Scale: The
gain_factoris calculated using the formula10^(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_factorto apply the gain. -
Write the Modified Audio File: The modified audio signal
audioOutis saved to a new file usingaudiowrite.
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.