how to run function with struct

2 Ansichten (letzte 30 Tage)
Kamil Kacer
Kamil Kacer am 30 Okt. 2020
Kommentiert: Kamil Kacer am 30 Okt. 2020
% How do I ran this function using struct other function midFeaures are included in matlab
% I cannot run the struct
function [midFeatures, Centers, stFeaturesPerSegment] = ...
featureExtractionFile(signal, stWin, stStep, mtWin, mtStep, featureStatistics)
% function [midFeatures, Centers, stFeaturesPerSegment] = ...
% featureExtractionFile(fileName, stWin, stStep, mtWin, mtStep, ...
% featureStatistics)
%
% [mtFeatures, centers] = featureExtractionFile(fileName, ...
% 0.040, 0.040, 2.0, 1.0, {'mean','std'});
%
% This function reads a struct element and computes
% audio feature statitstics on a mid-term basis.
%
% ARGUMENTS:
% - signal: audio signal (struct)
% - stWin: short-term window size (in seconds)
% - stStep: short-term window step (in seconds)
% - mtWin: mid-term window size (in seconds)
% - mtStep: mid-term window step (in seconds)
% - featureStatistics: list of statistics to be computed (cell array)
%
% RETURNS
% - midFeatures [numOfFeatures x numOfMidTermWins] matrix
% (each collumn represents a mid-term feature vector)
% - Centers: representive centers for each
% mid-term window (in seconds)
% - stFeaturesPerSegment cell that contains short-term feature sequences
%
% (c) 2014 T. Giannakopoulos, A. Pikrakis
% convert mt win and step to ratio (compared to the short-term):
mtWinRatio = round(mtWin / stStep);
mtStepRatio = round(mtStep / stStep);
readBlockSize = 60; % one minute block size:
% get the length of the audio signal to be analyzed:
% ndret til struct brug!
a = signal.Filt_data;
fs = signal.SampleRate;
numOfSamples = length(a);
BLOCK_SIZE = round(readBlockSize * fs); % Antal samples per minut
curSample = 1;
count = 0;
midFeatures = [];
Centers = [];
stFeaturesPerSegment = {};
while (curSample <= numOfSamples) % while the end of file has not been reahed
% find limits of current block:
N1 = curSample;
N2 = curSample + BLOCK_SIZE - 1;
if (N2>numOfSamples)
N2 = numOfSamples;
end
tempX = signal.Filt_data(N1:N2,:); % ndret til struct brug!
% STEP 1: short-term feature extraction:
Features = stFeatureExtraction(tempX, fs, stWin, stStep);
% STEP 2: mid-term feature extraction:
[mtFeatures, st] = mtFeatureExtraction(...
Features, mtWinRatio, mtStepRatio, featureStatistics);
for (i=1:length(st))
stFeaturesPerSegment{end+1} = st{i};
end
Centers = [Centers readBlockSize * count + (0:mtStep:(N2-N1)/fs)];
midFeatures = [midFeatures mtFeatures];
% update counter:
curSample = curSample + BLOCK_SIZE;
count = count + 1;
end
if (length(Centers)==1)
Centers = (numOfSamples / fs) / 2;
else
C1 = Centers(1:end-1);
C2 = Centers(2:end);
Centers = (C1+C2) / 2;
end
if (size(midFeatures,2)>length(Centers))
midFeatures = midFeatures(:, 1:length(Centers));
end
if (size(midFeatures,2)<length(Centers))
Centers = Centers(:, 1:size(midFeatures,2));
end

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 30 Okt. 2020
The code expects the first parameter to be a struct with fields Filt_Data and SampleRate . Filt_Data is expected to be a 2D array with one column per channel.
  5 Kommentare
Walter Roberson
Walter Roberson am 30 Okt. 2020
featureExtractionFile was originally literally a function to extract features from a file.
Someone along the way changed the code to expect a structure of information, and to not accept a file, but did not change the name of the function.
So your code
featureExtractionFile(signal, 0.05, 0.025, 2, 1, {'mean', 'median', 'std', 'stdbymean', 'max', 'min'});
is extracting features from the signal struct, and is throwing away the extracted features (you do not assign to output).
Then your code
plotFeaturesFile('audio.wav', 6)
passes a file name into a function that sets up, and does
[midFeatures, Centers, stFeaturesPerSegment] = featureExtractionFile(...
wavFileName, shortTermSize, shortTermStep, midTermSize, midTermStep, Statistics);
but that is going to fail because featureExtractionFile now has no ability to extract information from files, and instead needs to extract from a structure.
However when I look at the code, it looks to me as if you could probably do
plotFeaturesFile(signal, 6)
as the code does not validate that it has been passed a file, so you can pass it a struct to pass to featureExtractionFile
It is a mystery to me as to why someone would modify the featureExtractionFile code to require a struct instead of a file without renaming the function. It would have made a lot more sense to have created a new featureExtractionStruct (that does what the current one does with struct input), and to have made featureExtractionFile into a routine that accepted a file name and read the audio data from it and created the struct and called featureExtractionStruct .
Kamil Kacer
Kamil Kacer am 30 Okt. 2020
Maaaaaan It worked thats like 1000$ worth advice
Thank you so much from SVK

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by