0

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');
Moosli
  • 3,140
  • 2
  • 19
  • 45
  • Possible duplicate of [Obtain the envelop of a signal using MATLAB](http://stackoverflow.com/questions/17780748/obtain-the-envelop-of-a-signal-using-matlab) – Jørgen Mar 22 '17 at 11:38
  • It's not a duplicate. He is working with sum. But i need to downsample the Audio file and make an envelope over each sample point. – Moosli Mar 22 '17 at 12:38
  • The envelope represent the global shape of the signal. It seems odd to me to talk about envelope over each sample point – PatriceG Mar 22 '17 at 13:20
  • @sandoval31 it's needed for an EEG measurement. How would you do it better? Can you give me a hint? – Moosli Mar 22 '17 at 13:54
  • @Moosli ok, so you meant "channel" by "sample point". I suppose then you need to keep all channel and compute the envelope on each of them – PatriceG Mar 22 '17 at 14:58
  • 1
    Ad @sandoval31 says you can still use a Hilbert transform. `envelope = abs(hilbert(wa));`, therefore it could be a duplicate. Otherwise, if you have access to Matlab R2015b or newer, consider using [envelope](https://se.mathworks.com/help/signal/ref/envelope.html) – Jørgen Mar 22 '17 at 19:01

1 Answers1

0

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.

Community
  • 1
  • 1
PatriceG
  • 3,851
  • 5
  • 28
  • 43
  • thanks. Would that be something like Envelope=abs(hilbert(wa)); Because that don't looks like en envelope for me. And does it matter that my va file has to columns because it's stereo? – Moosli Mar 22 '17 at 12:51
  • Yes, you have to process the two channels separately, or maybe merge the channels with average value. Usually, you can take only one of the two channels. – PatriceG Mar 22 '17 at 13:14