How do i plot an audio file with an interval in ms?

Hi! I am currently using matlab 2021 version. I want to know how would I plot an audio file in the interval of 20 miliseconds of my choice for example the audio recording is 10 seconds long but, I want to plot only from 0 to 20 miliseconds. How would I do that? This is my code right now
recObj = audiorecorder;
Fs=8000;
filename = sprintf('myAudioData.wav');
disp('Start speaking.')
recordblocking(recObj, 10);
disp('End of Recording.');
doubleArray = getaudiodata(recObj);
audiowrite(filename,doubleArray,Fs);
[x,Fs] = audioread('myAudioData.wav');
[m,n]=size(x);
dt=1/Fs;
t=dt*(0:m-1);
idx = (t>=1.030) & (t<1.032);
selected_t = t(idx);
selected_x = x(idx,:);
plot(selected_t, selected_x);

 Accepted Answer

Using the sample frequency of the audio data (Fs), this code will extract and plot the first 20 ms:
[x, Fs] = audioread('AudioSample.wav'); % test audio file
ts = 1 / Fs; % duration of each sample
n = round(0.02/ts); % number of samples in 1st 20 ms
plot(x(1:n)); % plot 1st 20 ms of audio file

5 Comments

what if the interval of 20 ms is in the middle?
the middle of the audio say 3.000s to 3.020
In this case you need to compute the start and end index of the segment of interest. This can be done as follows:
[x, Fs] = audioread('AudioSample.wav');
ts = 1 / Fs; % duration of each sample
% start and end time of segment of interest (seconds)
t1 = 3.000;
t2 = 3.020;
% start and end index in audio data of segment of interest
idx1 = round(t1/ts)+1;
idx2 = round(t2/ts)+1;
% plot it!
plot(x(idx1:idx2));

Sign in to comment.

More Answers (1)

Categories

Find more on Measurements and Spatial Audio in Help Center and File Exchange

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!