Help with plotting histogram from audio file
    조회 수: 7 (최근 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!


