From audio signal to mean of frequency responses
6 views (last 30 days)
Julia Greeven on 20 Jul 2022
Im not very advanced in Matlab.
I have an audio signal of exhaling air and want to obtain the mean of frequency responses of that signal so that I can further evaluate the lung function by converting the mean of frequency responses to flow rate. The following steps I want to do
- Segment the file into 100 millisecond segments
- Convert each segment into frequency domain using Fast Fourier Transformation.
- Apply Butterworth filter on each segment to extract frequencies between 100 HZ and 1200 HZ.
- Calculate the mean of frequency responses between 100 HZ and 1200 HZ for each segment.
The following code I found that performs the first two steps. After that I found it difficult to apply a butterworth filter and calculate the mean of frecuency responses.
I hope someone can help me out here.
[data, fs] = audioread('test-kort3.wav');
% read exhalation audio wav file (1 channel, mono)
% frequency is 44100 HZ
% windows of 0.1 s and overlap of 0.05 seconds
WINDOW_SIZE = fs*0.1; %4410 = fs*0.1
array_size = length(data); % array size of data
numOfPeaks = (array_size/(WINDOW_SIZE/2)) - 1;
step = floor(WINDOW_SIZE/2); %step size used in loop
transformed = data;
k = 1;
t = 1;
g = 1;
o = 1;
% performing fft on each window and finding the peak of windows
WindowArray(j) = transformed(i);
j = j+1;
i = i +1;
Y = fft(WindowArray);
p = abs(Y).^2; %power
Christopher McCausland on 20 Jul 2022
A butterworth filter can be applied with the butter() command as detailed in the documentation.
A more intuative approch to get started is to use the design filter fucntion within MATLAB, This would look something like;
function filteredOutput = bandpassFilter(data,fs,LC,HC,Order)
% Create bandstop filter and filter data
% data - a vector of numerical data to filter; fs - sampling freq of data;
% LC - Low cut point; HC - High cut point; Order - specified filter order
% (or as close to order as possiable)
filter = designfilt('bandpassiir','FilterOrder',Order, ...
% Optional check what the filter looks like with freqz()
% use filtfilt to apply the filter to the data and ensure zero-phase shift
% at the cost of doubling the filter order
filteredOutput = filtfilt(filter,data);
The only downside to the filter function is that it will try its best to match the required parameters, however if the response is not mathematically possiable it will either try and fit something as close to what you asked as possiable or throw an error.
@Star Strider also has several great examples on the topic if you want to try generating a butterworth from scratch too, its a great exercise in digital signal processing.
In terms of calculating the mean response I assume you are talking about comparing each window, this is a more probmatic topic (but not impossiable) as you are trying to reduce the signal vector into one value. I would avoid mean and instead focus on median as it is less prone to skewing. Please give me some more information as to what you want the output to look like from this and I will help.