Search code examples
matlabaudioenvelope

Matlab downsampling and Envelope extraction from wav File


I have a question to the Code below, because i'm not sure if i programmed everything right and that the Code really does what i want him to do.

I have a Audiofile "03 Black Smoke.wav", this Audiofile i have to downsample to 500 Hz. After that, i have to cut the Audiofile to the max length of 3 min.

Ist the code doing the downsampling correctly?

And can someone give me a hint, how i could extract the envelope from the input_cut.wav File for each sample point?

% Downsampling of the audio file to ensure compatibility with EEG-Data
% Extracts musical features (envelope) of a song.
% Input: Audio File
% Author: A. B.
% Date: 06.02.2017

clc;
clear;

% get infos (e.g. sample rate)
info = audioinfo('03 Black Smoke.wav');
[input,Fs] = audioread('03 Black Smoke.wav');
plot(input)

% plot in frequency domain
n = length(input)-1;
f = 0:Fs/n:Fs;
wavefft = abs(fft(input)); 
figure (2);
plot(f,wavefft); 

% downsampling audio file
[P,Q] = rat(500/Fs);
abs(P/Q*Fs-500);
xnew = resample(input,P,Q);
figure (3);
plot(xnew);

% plot in frequency domain downsamplet 
n = length(xnew)-1;
f2 = 0:Fs/n:Fs;
wavefft2 = abs(fft(xnew)); 
figure (4);
plot(f2,wavefft2); 

% save downsampled audio file
audiowrite('xnew.wav',xnew,500);
info_ds = audioinfo('xnew.wav');

[input_ds,Fs_ds] = audioread('xnew.wav');
figure (5);
plot(input_ds);

% cut the file to 3 min length and save
samples = [1,180*500];
[input_cut, Fs_cut] = audioread('xnew.wav', samples);

audiowrite('input_cut.wav',input_cut,500);
info_cut = audioinfo('input_cut.wav');

% % play audio with different sample rates (control)
% P_orig = audioplayer(input,44100);
% P_new = audioplayer(input_ds,500);
% play(P_orig);
% stop(P_orig);
% play(P_new);
% stop(P_new);

% extract musical features - envelope
[wa,fs]=wavread('input_cut.wav');

Solution

  • There are several ways to compute the envelope. You could use an Hilbert transform, or more simple, just took the absolute value of each sample.

    For the Hilbert transform option, you will find more info here.