‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎Code‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎

Script for playing audio at a loudness relative to background noise (MATLAB)

This MATLAB script was used to dynamically adapt to changes in background noise intensity for my most recent Human Factors Ergonomics Society (HFES) Proceedings Paper entitled: Adaptive Auditory Alerts for Smart In-Vehicle Interfaces. It is also hosted on my GitHub.

In this case, the chosen approach was to calculate the background noise intensity and modify the sound signal to be presented at 10 dB greater than the noise.

% Please note that this was largely written by Dr. Justin MacDonald at New Mexico State University. 

signal = signal(:,1); % make mono signal

rms_signal = sqrt(mean(signal.^2)); % calculate rms of the signal

% These are pre-measured values which indicate the rms input to the mic
% when it is listening to an 87.5 dB signal. rms_output_reference indicates
% the rms necessary to produce a 87.5 dB sound out of the speaker array.

rms_input_reference = 0.0586;
rms_output_reference = 0.5773;

% These lines set up the mic to read in the background noise
mic_recorder = audiorecorder(Fs,16,1);
recordblocking(mic_recorder, 0.5);
bg_noise = getaudiodata(mic_recorder);

% This filters the background noise with an A-weighted filter, which is 
% used by the sound level meter that was used to calibrate. This allows for
% the conversion of the microphone signal rms to a dB value.
bg_noise = filterA(bg_noise, Fs);

rms_bg = sqrt(mean(bg_noise.^2); % calculate rms of background noise from microphone input

dB_bg = 87.5 + 20*log10(rms_bg/rms_input_reference); % calculate the dB of background noise

% The following if statement ensures that alerts are not too quiet or particularly loud
if dB_bg < 40
  dB_outpt = 50;
elseif dB_bg > 60
  dB_output = 70;
else
  dB_output = dB_bg + 10;
end

% Assess the needed rms output so that the sound produced by the speakers will have 
% the necessary dB output
rms_output_needed = rms_output_reference .* 10.^((dB_output - 87.5/20);

% Rescale the signal
signal = (rms_output_needed / rms_signal) .* signal;

% Play the alert
sound(signal, Fs);

RMS Standardization (MATLAB)

This MATLAB script (posted entirely below) was used to standardize auditory stimuli to have an identical average amplitude across the entirety of the auditory clip. It is also hosted on my GitHub.

% The code below takes audio wav files and normalizes them
% to have an equal average loudness across time.
% This script was used to normalize audio files for the manuscript
% titled: "Towards a Better Understanding of In-Vehicle Auditory Warnings and Background Noise."
% authors: Edin Šabić1, Jing Chen2, and Justin A. MacDonald1
% New Mexico State University (1), Old Dominion University (2)

m = zeros(29,1);

for wave_number = 1:29 % assumes that your files are named 1.wav, 2.wav, etc.
    
    % read in the sound file
    sound_in = audioread([int2str(1) '.wav']);
    sound_in = sound_in(:,1);
    
    % calculate the rms of the original wav file
    rms = sqrt(mean(sound_in.^2));
    
    %rescale the sound so that the rms is 1
    sound_in = sound_in ./ rms;
    
    % get the max sample value for the rescaled sound
    m(wave_number) = max(abs(sound_in));
    
    % repeat this process for all of your other sound files
end

%this gives you the max sample across all of your stimuli
max_sample = max(m);

%rescale all 29 stimuli so that none of them have samples that exceed +/- 1
for wave_number = 1:29

    % read in the sound file
    [sound_in,Fs] = audioread([int2str(wave_number) '.wav']);
    
    % calculate the rms of the original wav file
    rms = sqrt(mean(sound_in.^2));
    
    %rescale the sound so that the rms is 1
    sound_in = sound_in ./ rms;
    
    % rescale the sound 1 more time
    sound_in = sound_in ./ max_sample;
    audiowrite([int2str(wave_number) '_out.wav'],sound_in,Fs);
    
end
%d bloggers like this: