create an average spectra from multiple spectra in matlab

Assuming you have multiple spectra with the same frequency range and number of frequency points, you can create an average spectrum as follows:

main.m
% example data
spectra = randn(100, 10); % 100 spectra with 10 frequency points each

% calculate average
avg_spectrum = mean(spectra, 1);

% plot original spectra and average spectrum
figure
hold on
plot(spectra')
plot(avg_spectrum, 'k', 'linewidth', 2)
hold off
xlabel('Frequency')
ylabel('Amplitude')
legend('Spectra 1', 'Spectra 2', 'Spectra 3', '...', 'Average Spectrum')
377 chars
16 lines

In this code, spectra is a matrix where each row is a spectrum and each column is a frequency point. We can calculate the average spectrum by taking the mean of spectra along the first dimension (1), which corresponds to the rows. This results in a row vector containing the average amplitude at each frequency point.

To visualize the result, we can plot each original spectrum as well as the average spectrum. In the plot command, we use spectra' to plot each row of spectra as a separate line, and then plot(avg_spectrum, 'k', 'linewidth', 2) to plot the average spectrum in black with a thicker line. The xlabel, ylabel, and legend commands are used to label the axes and create a legend.

Note that if your spectra have different frequency ranges or number of frequency points, you will need to interpolate or resample them first so that they are aligned before averaging.

related categories

gistlibby LogSnag