ADSR envelope shaping on audio file

조회 수: 6 (최근 30일)
John D
John D 2020년 11월 6일
댓글: Mathieu NOE 2020년 11월 7일
I'm trying to apply ADSR envelope shaping to an audio file as shown below.
load handel y Fs;
N = 1.5 * Fs;
t = [0:N-1]/Fs;
x = y/Fs;
env = interp1([0 0.1 0.3 1.1 1.5], [0 1 0.4 0.4 0], t);
y = env .* x;
sound(y,Fs)
However, when performing the .* calculation, MATLAB freezes as the calculation is too large.
Could you assist in solving this problem? Thanks in advance.
  댓글 수: 2
Mathieu NOE
Mathieu NOE 2020년 11월 6일
hi can you sahre the data file ?
John D
John D 2020년 11월 6일
"handel" is a built in piece of audio

댓글을 달려면 로그인하십시오.

채택된 답변

Mathieu NOE
Mathieu NOE 2020년 11월 7일
hi
so finally found the bug is due to improper t (time) definition
t is supposed to span from 0 to 1.5 according to the envelope specs, and have N samples
NB : the t and env lines must both have "1.5" in common otherwise there will be error msg
from there , everything runs smooth, the env is applied to the entire audio file
N);
see below :
load handel y Fs;
N = length(y);
t = linspace(0,1.5,N); % must be consistent with env definition below : [0 0.1 0.3 1.1 1.5]
env = interp1([0 0.1 0.3 1.1 1.5], [0 1 0.4 0.4 0], t);
env = env(:);
y = env .* y(:);
sound(y,Fs)
%
if you want to generalize the code and make it more robust , you should consider define the interp1 arguments as vectors
and generate t according to something like :
vect1 = [0 0.1 0.3 1.1 1.5];
vect2 = [0 1 0.4 0.4 0];
env = interp1(vect1, vect2, t);
t = linspace(vect1(1),vect1(end),N);
% so improved code suggestion
load handel y Fs;
N = length(y);
vect1 = [0 0.1 0.3 1.1 1.5];
vect2 = [0 1 0.4 0.4 0];
t = linspace(vect1(1),vect1(end),N);
env = interp1(vect1, vect2, t);
env = env(:);
y = env .* y(:);
sound(y,Fs)
  댓글 수: 2
John D
John D 2020년 11월 7일
Thanks very much for your help. That solved the problem!
Mathieu NOE
Mathieu NOE 2020년 11월 7일
glad it helped !

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2020년 11월 6일
ok
so simple problem of vector dimensions mismatch in your code
the env vector is either too short or the vector on which it is applied is too long
you have to modify one or the other
below my 2 cents suggestion, assuming N was supposed to be the number of samples extracted from the original file
also I didn't get why the line : x = y/Fs; this will simply reduce the amplitude of the output signal to almost zero
load handel y Fs;
N = round(1.5 * Fs); % why 1;5 ??
t = [0:N-1]/Fs;
% x = y/Fs; % why ??
env = interp1([0 0.1 0.3 1.1 1.5], [0 1 0.4 0.4 0], t);
env = env(:);
x_truncated = y(1:N); % only the first N samples of the audio file
x_truncated = x_truncated(:);
y = env .* x_truncated;
sound(y,Fs)
  댓글 수: 3
Mathieu NOE
Mathieu NOE 2020년 11월 6일
Was the size of the envelope supposed to equal the length of the total audio file ?
if yes you simply have to correct the t (time) vector length
N = length(y) ; (and not N = round(1.5 * Fs))
John D
John D 2020년 11월 7일
Yes, 1.5 is the initial size of the envelope, which is then scaled up to fit the length of the audio file.
I've altered the code based on your suggestion, however, I would rather not truncate the final result as then the envelope will only be applied to part of the audio.
load handel y Fs;
N = length(y);
t = [0:N-1]/Fs;
env = interp1([0 0.1 0.3 1.1 1.5], [0 1 0.4 0.4 0], t);
y = env .* y;
sound(y,Fs)
I'm still getting a similar error:
Requested 73113x73113 (39.8GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a
long time and cause MATLAB to become unresponsive.
Do you have any ideas what's up? Thanks.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with Audio Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by