example of a fourier analysis plotting - graphis and logical operators in matlab

Here's an example of how to perform Fourier analysis and plot the results in MATLAB:

main.m
% Generating signal
fs = 1000;          % Sampling frequency (Hz)
t = (0:fs-1)/fs;    % Time vector (s)
f1 = 5;             % Signal frequency (Hz)
f2 = 15;            % Signal frequency (Hz)
signal = sin(2*pi*f1*t) + sin(2*pi*f2*t);

% Perform Fourier transform
y = fft(signal);
f = (0:length(signal)-1)*fs/length(signal);

% Plot original signal and Fourier spectrum
figure;
subplot(2,1,1);
plot(t,signal);
xlabel('Time (s)');
ylabel('Amplitude');
title('Original Signal');
subplot(2,1,2);
plot(f,abs(y));
xlim([0,max(f)/2]);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Fourier Spectrum');
602 chars
25 lines

This code generates an example signal consisting of two sine waves with frequencies of 5 and 15 Hz. The signal is then Fourier transformed using the fft function. The resulting spectrum is then plotted using the plot function, with the x-axis representing frequency and the y-axis representing magnitude.

To illustrate the use of logical operators in MATLAB, here's an example of how to use them to filter out unwanted frequencies from the Fourier spectrum:

main.m
% Filter out frequencies outside of a certain range
min_freq = 8;       % Minimum frequency to keep (Hz)
max_freq = 12;      % Maximum frequency to keep (Hz)
y_filtered = y;
y_filtered(f < min_freq | f > max_freq) = 0;

% Plot filtered spectrum
figure;
plot(f,abs(y_filtered));
xlim([0,max(f)/2]);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Filtered Fourier Spectrum');
381 chars
14 lines

This code uses the logical operators | (or) and <, > (less than, greater than) to filter out any frequencies outside of a certain range. The resulting filtered spectrum is then plotted.

gistlibby LogSnag