-
Notifications
You must be signed in to change notification settings - Fork 14
/
CreateLoudnessFeature.m
42 lines (36 loc) · 1.46 KB
/
CreateLoudnessFeature.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function loudnessFeature = CreateLoudnessFeature(audioData, audioFS, loudnessFS)
% function loudnessFeature = CreateLoudnessFeature(audioData, audioFS, loudnessFS)
% Compute the loudness (intensity) of an audio signal by averaging the
% squared energy.
%
% Given audioData at a sample rate of audioFS, compute the loudnessFeature
% of the sound. This is done by finding the RMS energy within +/-1
% windowSize (defined below) samples of the center point. Then, if desired
% average this calculation over delta frames to smooth the energy
% calculation.
% By Malcolm Slaney, Google Machine Hearing Project
audioEnergy = audioData .^ 2;
windowSize = 1.5/loudnessFS; % in seconds
N = round(length(audioData)/audioFS*loudnessFS);
loudnessFeature = zeros(N, 1);
for i=1:N
t = i/loudnessFS; % Center of window In seconds
b = max(1,round((audioFS*(t-windowSize))));
e = min(length(audioData), ...
max(1, round((audioFS*(t+windowSize)))));
m = mean(audioEnergy(b:e));
if isnan(m)
error('Got NaN when computing Loudness Feature');
end
loudnessFeature(i) = m;
end
if 0 % Test Code
[tap,fs] = audioread('tapestry.wav');
lfs = 100;
loudness = CreateLoudnessFeature(tap', fs, lfs);
h = plot((1:length(tap))/fs, tap, ...
(1:length(loudness))/lfs, sqrt(loudness));
xlabel('Seconds'); ylabel('Amplitude');
set(h(2), 'LineWidth', 6)
end