Phase Shift of Sinusoidal Functions- MATLAB Plot Tutorial
What Is Phase Shift in Sinusoidal Functions?
Phase shift describes how far a wave moves horizontally from its standard position. In the equation y = A·sin(Bx - C) + D, the term (-C) inside the sine function causes the shift.
That's it. No philosophy here. Just a horizontal displacement of your wave.
You encounter phase shift everywhere: audio signals, AC circuits, mechanical vibrations, climate data. If something oscillates, phase shift is probably relevant.
The Standard Form You Need to Know
Every sinusoidal function follows this structure:
y = A·sin(B·(x - C)) + D
- A = Amplitude (vertical stretch)
- B = Period (controls wave frequency)
- C = Phase shift (horizontal move, positive C shifts RIGHT)
- D = Vertical shift (moves up or down)
Most people get phase shift backwards. Watch the sign carefully. If your equation is sin(x - π/4), the wave shifts right by π/4. If it's sin(x + π/4), it shifts left by π/4.
MATLAB Setup for Plotting Sinusoids
Before plotting anything, define your x-axis properly. This matters more than most beginners realize.
% Basic setup
x = linspace(0, 4*pi, 1000); % 0 to 4π, 1000 points for smoothness
y = sin(x);
Use linspace instead of manually incrementing. It gives you even spacing without extra work. The 1000 points ensure smooth curves without jagged edges.
Plotting Phase Shifted Sinusoids: The Direct Method
The simplest approach: modify the input variable x.
% Phase shift by π/2 to the right
x = linspace(0, 4*pi, 1000);
phase_shift = pi/2;
y1 = sin(x); % No shift
y2 = sin(x - phase_shift); % Shifted right by π/2
figure;
plot(x, y1, 'b-', 'LineWidth', 2); hold on;
plot(x, y2, 'r--', 'LineWidth', 2);
xlabel('x (radians)');
ylabel('Amplitude');
title('Phase Shift Comparison');
legend('sin(x)', 'sin(x - π/2)');
grid on;
The blue line is your standard sine. The red dashed line is shifted right. Notice how the peaks and zeros moved.
Shifting Left
To shift left, add instead of subtract:
y3 = sin(x + phase_shift); % Shifted left by π/2
Using the General Form in MATLAB
If your function comes in the form A·sin(B·x - C) + D, extract the phase shift correctly:
% General form: A*sin(B*x - C) + D
% Phase shift = C/B
A = 2; B = 2; C = pi/3; D = 1;
x = linspace(0, 4*pi, 1000);
y = A * sin(B*x - C) + D;
plot(x, y);
title('y = 2·sin(2x - π/3) + 1');
The phase shift here is C/B = (π/3)/2 = π/6 to the right.
Comparing Phase Shift Methods
Different situations call for different approaches. Here's what works:
| Method | Code | Best For |
|---|---|---|
| Direct subtraction | sin(x - shift) | Simple shifts, teaching |
| Variable transformation | x_shifted = x - shift | Multiple plots, comparisons |
| General form extraction | A*sin(B*x - C) + D | Equations from textbooks |
| Phase object | phaseshift() function | Signal processing |
Plotting Multiple Phase-Shifted Waves
Visualizing different phase shifts together clarifies the concept:
x = linspace(0, 2*pi, 1000);
shifts = [0, pi/4, pi/2, 3*pi/4, pi];
colors = ['k', 'r', 'g', 'b', 'm'];
figure;
for i = 1:length(shifts)
plot(x, sin(x - shifts(i)), colors(i), 'LineWidth', 2); hold on;
end
xlabel('x'); ylabel('Amplitude');
legend({'0', 'π/4', 'π/2', '3π/4', 'π'}, 'Location', 'best');
title('Sine Waves with Increasing Phase Shift');
grid on;
This loop approach is cleaner than writing five separate plot commands. The waves stack up, and you can see exactly how each shift moves the curve.
Phase Shift with Cosine
Cosine is just sine with a built-in phase shift of π/2. Keep this in mind:
- cos(x) = sin(x + π/2)
- sin(x) = cos(x - π/2)
x = linspace(0, 2*pi, 1000);
plot(x, sin(x), 'b-', 'LineWidth', 2); hold on;
plot(x, cos(x), 'r--', 'LineWidth', 2);
legend('sin(x)', 'cos(x)');
Cosine peaks where sine crosses zero going up. They're the same wave, just phase-shifted.
Common Mistakes That Ruin Your Plots
1. Wrong sign direction
People constantly confuse left and right shifts. Remember: x - C shifts right. x + C shifts left. The sign inside the function is counterintuitive.
2. Forgetting to convert degrees to radians
MATLAB works in radians. If you want 90°, use pi/2, not 90.
% WRONG
sin(x - 90) % MATLAB ignores this
% CORRECT
sin(x - pi/2) % or deg2rad(90)
3. Not enough points on the x-axis
Less than 500 points produces visible edges. Use at least 1000 for smooth curves.
4. Confusing phase shift with period change
The coefficient B affects both period and phase shift when written as B·x - C. Always extract them separately: period = 2π/B, phase shift = C/B.
Practical Example: Sound Wave Analysis
Suppose you have two sound signals with different phase shifts and you want to visualize their superposition:
% Two signals
fs = 1000; % Sampling frequency
t = 0:1/fs:0.1; % 0 to 0.1 seconds
f = 440; % 440 Hz (A4 note)
% Signal 1: original
signal1 = sin(2*pi*f*t);
% Signal 2: phase shifted by 45 degrees
phase = deg2rad(45);
signal2 = sin(2*pi*f*t - phase);
% Superposition
combined = signal1 + signal2;
figure;
subplot(3,1,1); plot(t, signal1); title('Signal 1');
subplot(3,1,2); plot(t, signal2); title('Signal 2 (shifted 45°)');
subplot(3,1,3); plot(t, combined); title('Combined Signal');
xlabel('Time (s)');
The combined signal shows constructive and destructive interference patterns. This is real-world signal processing.
Quick Reference: Phase Shift Commands
- linspace(a, b, n) — Create evenly spaced x-axis
- deg2rad(degrees) — Convert degrees to radians
- rad2deg(radians) — Convert radians to degrees
- pi — π constant in MATLAB
- hold on — Overlay multiple plots
- grid on — Add gridlines for readability
When Phase Shift Matters Most
You need to care about phase shift in these situations:
- Signal processing — Filters and Fourier transforms
- Control systems — Bode plots, stability analysis
- AC circuit analysis — Voltage-current relationships
- Oscillations — Damped systems, resonance
- Data fitting — Sinusoidal regression on real data
In each case, getting the phase shift wrong means your model doesn't match reality. There's no workaround here—you either calculate it correctly or your results are wrong.
Bottom Line
Phase shift moves your wave horizontally. The sign inside the sine function determines direction. MATLAB handles this through simple arithmetic on your x variable.
Plot, compare, verify. That's the workflow. No magic involved.