phase as a function of frequency for forced oscillationz in matlab

To compute the phase of a forced oscillation as a function of frequency in MATLAB, you can use the function angle on the Fourier transform of the signal. Here is an example code snippet:

main.m
% Define a sample signal with a forced oscillation
fs = 1000; % Sampling frequency
t = 0:1/fs:1-1/fs; % Time vector
f1 = 50; % Forced oscillation frequency
x = sin(2*pi*f1*t) + randn(size(t));

% Compute the Fourier transform of the signal
X = fft(x);

% Compute the frequency vector
df = fs/length(x); % Frequency resolution
f = 0:df:fs-df; % Frequency vector

% Compute the phase angle of each frequency component
phi = angle(X);

% Plot the phase angle as a function of frequency
plot(f, phi);
xlabel('Frequency (Hz)');
ylabel('Phase angle (rad)');
552 chars
21 lines

In this example, we first define a sample signal x with a forced oscillation at 50 Hz. We then compute the Fourier transform of the signal using the fft function, and the frequency vector using the sampling frequency and the length of the signal. Finally, we use the angle function to compute the phase angle of each frequency component of the signal.

We can then plot the phase angle as a function of frequency using the plot function. The resulting plot will show how the phase of the forced oscillation varies with frequency.

gistlibby LogSnag