Hi,
It seems like you're trying to define a function in MATLAB that calculates the Full Width at Half Maximum (FWHM) for a given signal within an object. The FWHM is a measure of the width of a signal at half of its maximum amplitude. However, there are a few issues with the code you've provided, and it does not actually calculate the FWHM.
Here is a corrected version of the function that attempts to find the indices where the signal crosses the half-maximum threshold and then calculates the width between these two points with a sample MATLAB script to validate the functionality:
signalData = [1, 2, 3, 4, 5, 4, 3, 2, 1];
Obj.Data = {{struct('mySignal', signalData)}};
Obj = FWHM(Obj, signalName);
fwhmField = [signalName '_FWHM'];
disp(['FWHM: ', num2str(Obj.Data{1}{1}.(fwhmField))]);
actualFWHM = Obj.Data{1}{1}.(fwhmField);
if actualFWHM == expectedFWHM
disp('The FWHM function is working correctly.');
disp('The FWHM function is not working correctly.');
end
The FWHM function is working correctly.
function Obj = FWHM(Obj, signalName)
for iCell = 1:length(Obj.Data)
for iFrame = 1:length(Obj.Data{iCell})
signalData = Obj.Data{iCell}{iFrame}.(signalName);
[maxValue, ~] = max(signalData);
indicesAboveHalfMax = find(signalData >= halfMax);
firstIndex = indicesAboveHalfMax(1);
lastIndex = indicesAboveHalfMax(end);
fwhmValue = lastIndex - firstIndex + 1;
Obj.Data{iCell}{iFrame}.([signalName '_FWHM']) = fwhmValue;
The provided MATLAB function "FWHM" calculates the Full Width at Half Maximum (FWHM) for a specified signal within each frame of data for each cell in an object. The function operates as follows:
- Iterate over cells and frames: The function loops through each cell ("iCell") in the "Obj.Data" array and then through each frame ("iFrame") within each cell's data.
- Extract signal data: For the current frame of the current cell, the function retrieves the data array corresponding to the signal specified by "signalName".
- Find maximum value: The function identifies the maximum value of the signal data array. It uses this value to calculate the half-maximum level ("halfMax"), which is half of the signal's peak value.
- Identify indices above half-maximum: The function then finds all indices of the signal data where the value is greater than or equal to the half-maximum level. These indices represent the points on the signal that are at or above half the peak amplitude.
- Determine FWHM bounds: From the indices identified in the previous step, the function determines the first and last indices ("firstIndex" and "lastIndex") where the signal is equal to or exceeds the half-maximum level. These indices mark the boundaries of the FWHM.
- Calculate FWHM: The FWHM value is calculated as the difference between the last and first indices plus one. This represents the width of the signal at the half-maximum level.
- Store FWHM in object: Finally, the calculated FWHM value is stored back into the object under the current cell and frame, with the key being the original "signalName" appended with "_FWHM".
The function modifies the original object by adding the FWHM information for each signal in each frame of each cell, allowing for further analysis or reporting of the signal's characteristics.
Further, for better understanding of the Object Oriented Programming and operations with objects in MATLAB, you can refer the following MathWorks documentations:
I hope this helps!