I have an audio file that gives me a long vector when I read it.I am asked to integrate the response over a specific time. My approach is to calculate time 'x' at which the vector reaches its 'ith'element. and then I will integrate it over the time. Can anyone guide

 채택된 답변

Star Strider
Star Strider 2017년 8월 30일

3 개 추천

To integrate a vector, use the trapz (link) or cumtrapz (link) function, depending on the result you want.
You probably need to calculate a time vector as well. Since audio files are column-major matrices (each column is a different channel), this will work to calcualte the time vector, with ‘y’ being your sound file, and ‘Fs’ your sampling frequency:
tv = linspace(0, size(y,1), size(y,1))'/Fs;

댓글 수: 6

Fourier
Fourier 2017년 8월 30일
So you mean tv is my time vector and I can simply integrate over time , let's say t=2 then y= 2/Fs*trapz(Input)
Yes, ‘tv’ is your time vector.
I was thinking more along these lines:
y_int = trapz(tv, y);
to integtrate the entire vector, or perhaps:
y_int_2 = trapz(tv(tv <= 2), y(tv <= 2));
to integrate up to ‘tv=2’, or:
y_int = cumtrapz(tv, y);
y_int_2 = y_int(find(tv <= 2, 1, 'last'));
to integrate up to ‘tv=2’.
Fourier
Fourier 2017년 8월 30일
okay many thanks, so finally can you tell how to integrate from tv=2 to tv=4
Variations on any of the previous syntaxes will work.
One option:
y_int = cumtrapz(tv, y);
y_int_2_4 = y_int(find(tv <= 4, 1, 'last')) - y_int(find(tv <= 2, 1, 'last'));
This is probably more efficient than doing the entire trapz calculation for every interval of interest. I didn’t time it.
Peter Druzba
Peter Druzba 2019년 10월 20일
Thanks, very helpfully !
Star Strider
Star Strider 2019년 10월 21일
@Peter Druzba — My pleasure!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

질문:

2017년 8월 30일

댓글:

2019년 10월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by