plot half the spectrum in hz for the following data sampled at 1 mhz, x=[21,43,21,45,47,48,47,45,66,66,66,66,66,98] in matlab

To plot half the spectrum in Hz for the given data x, we first need to obtain the frequency axis for the spectrum. Since the data is sampled at 1 MHz, the frequency resolution of the spectrum will be 1/N, where N is the length of the data.

To obtain the frequency axis, we can use the fft function in MATLAB, along with the Fs parameter set to 1 MHz. We can then take the first half of the resulting frequency vector and plot it against the magnitude of the one-sided spectrum.

Here's the code to accomplish this:

main.m
% Given data
x = [21,43,21,45,47,48,47,45,66,66,66,66,66,98];

% Obtain frequency axis
N = length(x);
Fs = 1e6;
f = (0:N-1) * Fs / N;
f = f(1:N/2);

% Compute one-sided spectrum
X = fft(x);
X = X(1:N/2);
X = abs(X);

% Plot half the spectrum in Hz
plot(f, X);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
307 chars
19 lines

This will plot the magnitude of the one-sided spectrum up to the Nyquist frequency (500 kHz), which is half the sampling frequency. Note that the data is not preprocessed in any way (e.g., windowing) so the resulting spectrum may have spectral leakage and other artifacts.

gistlibby LogSnag