Compare each spectrum to a reference spectrum.
3 views (last 30 days)
Show older comments
Hi all, say I have a reference signal (spectrum) and assume I have acquired 100 other spectra. I want to compare each of the 100 spectra to the reference spectrum. Is there a code for computing a conformity index by way of comparing each of the 100 spectrum to the reference?
Thank you.
1 Comment
Image Analyst
on 26 Aug 2023
There are lots of metrics for comparing curves (signals). For example RMS, Mean absolute deviation (MAD), median absolute deviation, etc. If you have a reference for the "conformity index" that gives the formula for it, then post it. We may have to write it if there is no built-in function for it in MATLAB like there is for rmse and mad.
Accepted Answer
Image Analyst
on 27 Aug 2023
Try something like this (untested) to compare spectro files to a ref spectro file using RMS. Adapt as needed.
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all spectroscopy files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.spc'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
%-----------------------------------------------------------------------------------------------------------
% Define the first data file as the reference file.
baseFileName = theFiles(k).name;
refFullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
data = GSSpcRead(refFullFileName);
refWaveNumbers = data.xaxis;
refIntensity = data.data;
%-----------------------------------------------------------------------------------------------------------
% Now compare this to all the others.
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
[folder, baseFileNameNoExtension, extension] = fileparts(fullFileName);
data = GSSpcRead(fullFileName); % Part of the GSTools open source suite of spectro tools.
waveNumbers = data.xaxis;
intensity = data.data;
% If larger wavenumbers are on the left, flip the signals.
if waveNumbers(2) < waveNumbers(1)
waveNumbers = fliplr(waveNumbers);
intensity = fliplr(intensity);
end
plot(waveNumbers, intensity, 'b-', 'LineWidth', 1)
grid on;
xlabel('Wavenumber', 'FontSize', 14);
ylabel('Intensity', 'FontSize', 14);
% Compute difference metric
e(k) = rmse(refIntensity, intensity)
end
GSTools is available here: https://www.mathworks.com/matlabcentral/fileexchange/9938-gstools
2 Comments
Image Analyst
on 27 Aug 2023
- You only have to install GSTools if you don't have a reader function for your data. If you already have some function to read in your data, then use that instead of GSTools. If you do use GSTools, it should be added to the path with subfolders.
- I have no idea how you're defining the reference file. I, very arbitrarily, just took the first file to be the reference. If you have some particular file, then just hard code it in or ask the user to specify the reference file with uigetfile
- That was left over pasted code from some boilerplate snippet I had. Basically you can read in the file with whatever function you want that works. Then once you have the data do whatever sort of analysis you want on the signal. Plot it, find spikes, find area under the curve, or whatever it may be.
More Answers (0)
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!