Help with plotting histogram from audio file

5 visualizaciones (últimos 30 días)
AZ0
AZ0 el 17 de Sept. de 2023
Respondida: Sandeep Mishra el 27 de Sept. de 2024
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 comentarios
AZ0
AZ0 el 18 de Sept. de 2023
Thanks so much for the feedback! Just edited it so that it takes the minimum between the total length of the sample and the finish time but it's still giving me issues. Do you know if this makes sense?
te = t + intervalSize;
%end time included in the histogram ( includes range of data from t to te)
start = floor(t*sampleRate);
%starting sample number
len = floor((sampleInfo.Duration * sampleRate)/2);
%total length of sample in seconds
finish = min(ceil(te*sampleRate), len);
%ending sample number
The new errors are:
Error using findpeaks
Expected Y to be a vector.
Error in findpeaks>parse_inputs (line 201)
validateattributes(Yin,{'double','single'},{'nonempty','real','vector'},...
Error in findpeaks (line 135)
= parse_inputs(isInMATLAB,Yin,varargin{:});
Walter Roberson
Walter Roberson el 18 de Sept. de 2023
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.

Iniciar sesión para comentar.

Respuestas (1)

Sandeep Mishra
Sandeep Mishra el 27 de Sept. de 2024
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:
  1. ‘audioread’ function: https://www.mathworks.com/help/releases/R2024a/matlab/ref/audioread.html#btiabil-1-y
  2. ‘audioinfo’ function: https://www.mathworks.com/help/releases/R2024a/matlab/ref/audioinfo.html#:~:text=character%20vector-,NumChannels,-Number%20of%20audio
  3. ‘findpeaks’ function: https://www.mathworks.com/help/releases/R2024a/signal/ref/findpeaks.html#bufbbs1-data
I hope this helps you in resolving the issue.

Community Treasure Hunt

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

Start Hunting!

Translated by