Assuming you have multiple spectra with the same frequency range and number of frequency points, you can create an average spectrum as follows:
main.m377 chars16 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.
gistlibby LogSnag