How to create noise on an audio file in matlab

Viewed 471

I wanted to create a noise on an audio file and have the ability to do this in any part of the audio file that I wanted to do .. Thank you for helping ....

I wrote this but it does not solve my problem:

load handel.mat;
hfile= 'handel.wav';
wavwrite(y, Fs, hfile);
nsamples=Fs;

[y, Fs , nbits, readinfo]= wavread(hfile,nsamples);

sound(y(round(2.85/4*end):end),Fs);
1 Answers

Adding Noise to a Specific Portion of a Signal

In this example, two variables named Start_Time and End_Time are used to indicate where the noise begins and ends in seconds. To convert these times to find the corresponding sample indices we can divide the Start_Time and End_Time by the Sampling_Period which is 1/Sampling/Frequency. After finding these indices, Start_Sample and End_Sample we can use matrix-indexing to extract the specific portion of the signal between the range and add our noise model. In this case, I simply used a random vector that ranges from -1 to 1 by implementing the rand() function. The code below also outlines an alternative option commented out that uses the awgn() function to apply Additive White Gaussian Noise (AWGN). Depending on your noise model this may differ. Hopefully, the code outlining how to extract/target a portion of the audio helps.

Audio Plot Comparison

Adding Random White Noise:

load handel.mat
File_Name = 'handel.wav';

audiowrite(File_Name,y,Fs);
[Original_Signal,Sampling_Frequency] = audioread('handel.wav');
Sampling_Period = 1/Sampling_Frequency;

%Selecting region to apply noise (1s to 3s)%
Start_Time = 1; %1 second%
End_Time = 3; %3 seconds%

Start_Sample = Start_Time/Sampling_Period;
End_Sample = End_Time/Sampling_Period;

%Random noise vector%
Noise_Strength = 0.1;
Noise_Vector = Noise_Strength.*(2*rand(1,End_Sample-Start_Sample+1)-1).';
Noisy_Signal = Original_Signal;

%Random Noise%
Noisy_Signal(Start_Sample:End_Sample) = Noisy_Signal(Start_Sample:End_Sample) + Noise_Vector;

%Additive White Gaussian Noise (AWGN)%
% Signal_To_Noise_Ratio = 20;
% Noisy_Signal(Start_Sample:End_Sample) = awgn(Noisy_Signal(Start_Sample:End_Sample),Signal_To_Noise_Ratio);

sound(Noisy_Signal,Sampling_Frequency);

Extension: Visually by Plotting

Audio_Properties = audioinfo('handel.wav');
Total_Samples = Audio_Properties.TotalSamples;
t = (0: Sampling_Period: Sampling_Period*(Total_Samples-1));

clf;
subplot(2,1,1); plot(t,Original_Signal);
title("Original Audio Signal");
xlabel("Time (s)"); ylabel("Amplitude");
ylim([1.2*min(Original_Signal) 1.2*max(Original_Signal)]);

subplot(2,1,2); plot(t,Noisy_Signal);
line([Start_Time Start_Time],[1.2*min(Noisy_Signal) 1.2*max(Noisy_Signal)],'color','r');
line([End_Time End_Time],[1.2*min(Noisy_Signal) 1.2*max(Noisy_Signal)],'color','r');
text((End_Time + Start_Time)/2,(max(Noisy_Signal)-min(Noisy_Signal))/2,"Noisy Region",'color','r','HorizontalAlignment','center');
title("Noise Added to Portion of Audio Signal");
xlabel("Time (s)"); ylabel("Amplitude");
ylim([1.2*min(Noisy_Signal) 1.2*max(Noisy_Signal)]);

Ran using MATLAB R2019b

Related