% perform the average and peak envelopes of an input signal,
% and then plot all three plots on a graph.
% usage: amplitude_envelopes ('*.wav')
% Author: Minh Anh Nguyen
% email: minhanhnguyen@q.com
function [amplitude_envelopes] = amplitude_envelopes ( file );
close all;% Close all figures (except those of imtool.)
clf % Clears the graphic screen.
% Reads in the sound file, into a big array called y.
y1=audioread(file);
[y1, fs1]=audioread(file);
size(y1);
left1=y1(:,1);
right1=y1(:,2);
% Normalize y; that is, scale all values to its maximum.
y = right1/max(abs(right1));
% If you want to hear the sound after you read it in
% Sound just plays a file to the speaker.
sound(y, 44100);
% Initialize peak and average arrays to be the same as the signal. We’re
% keeping three big arrays, all of the same length.
average_env = y;
max_env = y;
% Set the window lengths directly, in number of samples.
% the average should be smaller than the peak, since it will tend to
% flatten out if it gets too big.
% average and peak window size are two numbers, which can be played with
% to vary what the pictures will look like.
% Note: use two different window sizes so that we can have
% different kinds of resolution for peak and average charts.
average_window_size = 512;
peak_window_size = 10000;
% from the input signal, taking an average of the previous,
% average window size number of samples, and store that in the current
% place in the average array.
% Use a loop that starts at the end of the first window and goes to the
% end of the sound file, indicated by length(average_env).
% use "sum" command to take some range of a vector.
for k = average_window_size:length(average_env)
running_sum = sum(abs(y(k-average_window_size+1:k)));
average_env(k) = running_sum /average_window_size ;
end
% from the input signal, taking the maximum value of the previous
% peak_window_size number of samples.
for k = peak_window_size:length(max_env)
max_env(k)= max(abs(y(k-peak_window_size+1:k)));
end
% Plot the three "signals": the original in the color cyan, the
% peak in magenta, and the average in blue. The colors are the
% letters in single quotes.
plot(y, 'c')
hold on
plot (max_env, 'm')
hold on
plot(average_env, 'b')
title('Monochord: Signal, Average Signal Envelope, Peak Signal Envelope')
xlabel('sample number')
ylabel('amplitude (-1 to 1)')
legend('original signal', 'peak envelope', 'running average envelope', 0)
No comments:
Post a Comment