plot average spectra of multiple values in matlab

Assume you have a matrix X with size MxN where M is the number of frequency points and N the number of spectra to average. You can plot the average spectra in Matlab using the following code:

main.m
% Compute the average spectra
Y = mean(X, 2);

% Plot the spectra
figure;
plot(Y);
title('Average Spectra');
xlabel('Frequency Points');
ylabel('Magnitude');
158 chars
10 lines

The mean function is used to compute the average spectra across the second dimension of the matrix X, which corresponds to the multiple spectra. The resulting vector Y has size Mx1 and contains the average spectra.

Then, we plot the vector Y using the plot function. The title, xlabel, and ylabel functions are used to add labels to the plot.

You can also specify the frequency range of the spectra by providing a vector of M frequency points. For example:

main.m
% Define the frequency range
f = linspace(0, 1000, M);

% Plot the spectra with frequency axis
figure;
plot(f, Y);
title('Average Spectra');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
188 chars
10 lines

Here, we use the linspace function to create a vector of M linearly-spaced frequency points between 0 and 1000 Hz. We then plot the spectra Y against the frequency vector f, and add labels to the plot as before.

related categories

gistlibby LogSnag