I load an audio file and get sample and sampleRate with
[sample, sampleRate] = audioread(audiofile);
I want to get the length of the audio in seconds, how do I proceed?
Having the sampleRate, the inverse of it is the sampling time.
Ts=1/sampleRate;
So the time vector for your sampled data would be
time=(0:(length(sample)-1))*Ts;
Related
I am using audioread to play audio, now I would like play the track in different timestamps. What I have so far is:
[testSound,Fs] = audioread('test.wav');
sound(testSound,Fs);
Is it possible to somehow specify that the audio-track shall start at for example second 5? To be more specific, my audio sample test.wav is 45 second long, instead of playing the sound from the beginning, I would like to define where it should start playing.
Any help is much appreciated!
You can extract out a portion of the signal so that you're starting at the 5 second mark, then play it. Simply put, you'd start sampling at 5 times the sampling rate as the starting index all the way to the end then play the sound:
[testSound,Fs] = audioread('test.wav'); % From your code
beginSecond = 5; % Define where you want to start playing
beginIndex = floor(beginSecond*Fs); % Find beginning index of where to sample
soundExtract = testSound(beginIndex:end, :); % Extract the signal
sound(soundExtract, Fs); % Play the sound
Alternatively, since you're using audioread, you can actually specify where to start sampling the sound. You'd use the same logic above and specify the beginning and end of where to sample the sound in terms of samples. However, you'd need to know what the sampling rate is first so you'd have to call audioread twice to get the sampling rate, then finally the signal itself:
beginSecond = 5; % Define where you want to start playing
[~, Fs] = audioread('test.wav'); % Get sampling rate
beginIndex = floor(beginSecond*Fs); % Find beginning index of where to sample
[soundExtract, ~] = audioread('test.wav', [beginIndex inf]); % Extract out the signal from the starting point to the end of the file
sound(soundExtract, Fs); % Play the sound
AIM:
I need to synchronize the animation of two plots in Matlab.
PROBLEM:
The data of the two plots has been acquired with a variable sample rate.
SOLUTION:
I converted the time-stamps of the two datasets in duration objects (relative to the beginning of the streaming).
Now I want to plot the two datasets in a for loop.
For each loop, I want to show the datasets samples whose durations are within the elapsed time.
QUESTION:
How do I determine if the duration of a specific sample already happened or not?
CODE EXAMPLE:
here I simulate and sort 10 random durations (d1), and 1 random elapsed time (et). I want to find which durations are past the elapsed time.
`
% simulate elapsed time
et = calendarDuration(round(rand(1,6)*10));
% simulate data for plot 1
data_for_plot1 = rand(10,1);
% simulate durations for the samples in plot1
d1 = calendarDuration(sortrows(round(rand(10,6)*10)));
% find index of durations which are before the elapsed time
is_past = (d1-et)>0;
% plot the data
plot(data_for_plot1(is_past))
`
ERROR MESSAGE
is_past = (d1-et)>0;
Undefined operator '>' for input arguments of type 'calendarDuration'.
ALTERNATIVE SOLUTIONS:
It's my first time with duration and date objects, and I am hating every bit of it. If you have alternative solutions I would love to hear them.
Mind that the timestamps of data1 come as strings ('yyyy-MM-dd HH:mm:ss.SSS') and the timestamps of data2 come as double (eg: 42.525, 42 s and 525 ms).
Thank you for your help
You can use split function for this purpose.
Use is_past = split((d1-et),'time')> 0; instead of is_past = (d1-et)>0;
The input video length is 1min 56sec and Ouput audio length comes out to be 2 min 47 sec
file1='vipmen1.wav'; %o/p file name
hmfr=video.MultimediaFileReader(file_fullpath,'AudioOutputPort',true,'VideoOutputPort',false);
hmfw = video.MultimediaFileWriter(file1,'AudioInputPort',true,'FileFormat','WAV');
while ~isDone(hmfr)
audioFrame = step(hmfr);
step(hmfw,audioFrame);
end
close(hmfw);
close(hmfr);
You have to use the same sample rate for your output. Read the sample rate from the input and use this rate to write the output.
Solved!
% Function to Generate ECG of heart beat signal for specified duration
%---------------------------------------
function [Heartbeat,t] = ECG_Gen (HR,pulse_width,Amp,duration)
Fs = 48000;
delay = (60/HR);
t = 0 : 1/Fs : duration; % 48000 kHz sample freq for duration (secs)
d = 0 : delay : duration;
Heartbeat = Amp*pulstran(t,d,'tripuls',pulse_width);
I'm having problem outputting my generated Heart beat signals, when I play the signal using Sound in matlab and measure it on an external heart rate monitor. I get a different reading to the simulated value. But seem to be correct only at 60 Bpm to maybe 100 Bpm. Need to include heart rates up to 200 Bpm. In order words, I get a lot of unstable output at high Bpm.
Change
delay = ((60/HR)/2)-(0.5*pulse_width);
into
delay = 30/HR;
tripuls does not change anything to the time input t1, so the pulse width should not be subtracted from the time vector.
You can see this is correct by setting
pulse_width = 60e-4;
% (try)
pulse_width = 60e-10;
% (try again)
You should see that your results slowly go more and more towards the correct HR (provided your external equipment is capable of handling such short pulses).
I'm trying to extract information from a sound file in order to use it in a video classification algorithm I'm working on.
My problem is that I don't know how to work exactly with audio files in Matlab.
Below is what I need to accomplish:
open the audio file and get the sampling rate/frequency
I need to work on a window of 2 seconds so I have to loop over the file and get each 2 seconds as a window and then do the ftt (Fast-Fourier-Transform) on each window.
After that it is my turn to use these values to do what I want
any help would be appreciated
Thanks.
Following code may only give you some idea. You may need to determine another fft size, a windowing function like hamming etc.
To read a wav file:
[data, Fs] = wavread('path.wav');
Splitting to 2 sec windows and getting fft:
frameFFT = [];
timeStep = Fs*2;
for i=1:timeStep:length(data)-timeStep
frameFFT = [frameFFT; fft(data(i:i+timeStep-1),1024)];
end