Help with plotting histogram from audio file
6 次查看(过去 30 天)
显示 更早的评论
Hi! I've recently joined a physics lab and am trying to write some code. I'm looking to create a histogram of pulse heights at a given time interval but I'm getting the following. I'm new to MatLab and don't quite understand what's going wrong. Does anyone think that they might be able to help? Here are pictures and the code. Thanks!
%Height Matrix
function [dataMatrix] = HeightWithFrequency(audioFileLocation, secondsPerInterval, t)
sampleInfo = audioinfo(audioFileLocation);
%sample rate in hz
sampleRate = sampleInfo.SampleRate;
intervalSize = sampleRate*secondsPerInterval;
%total length of sample in seconds
te = t + intervalSize;
%end time included in the histogram ( includes range of data from t to te)
start = floor(t*sampleRate);
%starting sample number
finish = ceil(te*sampleRate);
%ending sample number
[y, ~] = audioread(audioFileLocation, [start, finish]);
%assigns y to be the first channel of the audioread
dataMatrix = findpeaks(y, 'MinPeakProminence', 0.06);
%matrix of peaks
dataMatrix = dataMatrix';
end
longrun = '/Users/.../.../.../.../.../fileName.flac';
time = 300;
interval = 30;
sizes = HeightWithFrequency(longrun, interval, time);
histogram(sizes, 30)
xlabel('size (units)')
ylabel('frequency')
title(['Frequency of cell sizes at time', num2str(time)]);
3 个评论
Walter Roberson
2023-9-18
Every time you calculate something using sample rates and so on then you have difficulties with uncertaintiies and rounding problems. You should be using the TotalSamples propertly in your min(). Note that the range to be passed is in samples not time.
回答(1 个)
Sandeep Mishra
2024-9-27
Hi AZ0,
I encountered a similar error using the ‘findpeaks’ function while running the code snippet in MATLAB R2024a.
Upon debugging the code, I discovered that the output from the ‘audioread’ function returns an m*n matrix, where ‘m’ represents the number of audio samples and ‘n’ indicates the number of audio channels present in the file.
To determine the number of channels in your audio file, you can utilize the ‘sampleInfo’ variable as shown below:
% Channel Info
numberOfChannels = sampleInfo.NumChannels;
Since the ‘findpeaks’ function takes a vector as input, a suitable solution is to extract peaks from a specific audio channel. You can use the following code snippet to obtain peaks from the first audio channel:
dataMatrix = findpeaks(y(:,1), 'MinPeakProminence', 0.06);
For more information, refer to the following MathWorks documentation:
- ‘audioread’ function: https://www.mathworks.com/help/releases/R2024a/matlab/ref/audioread.html#btiabil-1-y
- ‘audioinfo’ function: https://www.mathworks.com/help/releases/R2024a/matlab/ref/audioinfo.html#:~:text=character%20vector-,NumChannels,-Number%20of%20audio
- ‘findpeaks’ function: https://www.mathworks.com/help/releases/R2024a/signal/ref/findpeaks.html#bufbbs1-data
I hope this helps you in resolving the issue.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio and Video Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!