# Phase distortion demo

Illustration of phase distortion (change of waveform shape). It is demonstrated that a linear-phase filter can preserve the shape of the waveform.

Ivan Selesnick, selesi@nyu.edu

clear

## Create signal

N = 200;
n = 0:N-1;

om1 = 0.1*pi;
om2 = 0.2*pi;

x = cos(om1*n) + sin(om2*n);        % Input signal

figure(1)
clf
subplot(2, 1, 1)
plot(n, x)
title('Input signal')

## Filter 1

b1                                  % b1, a1 : coefficients of difference equation
a1

[H1, om] = freqz(b1, a1);           % Compute frequency response
f = om/(2*pi);                      % Frequency (cycles/sample)

figure(1)
clf
plot(f, abs(H1))
xlabel('Frequency')
title('Frequency response magnitude')
b1 =

-0.0312         0    0.2812    0.5000    0.2812         0   -0.0312

a1 =

1

## Output 1

The output signal looks the same as the input signal, except delayed. The input signal is in the pass-band of the filter.

y1 = filter(b1, a1, x);             % Apply filter 1 to signal x

figure(1)
clf
subplot(2, 1, 1)
plot(n, x)
title('Input signal')
subplot(2, 1, 2)
plot(n, y1)
title('Output of filter 1')

## Filter 2

b2
a2

[H2, om] = freqz(b2, a2);           % Compute frequency response

figure(1)
clf
plot(f, abs(H2))
title('Frequency response magnitude')
xlabel('Frequency')
b2 =

Columns 1 through 7

-0.0253    0.0523    0.1966   -0.0657   -0.3277    0.0293    0.2559

Columns 8 through 9

0.0523   -0.0312

a2 =

1.0000   -1.6736    0.8100

## Output 2

Output 2 differs in waveform shape from output 1. Why is that?

y2 = filter(b2, a2, x);             % Apply filter 2 to signal x

figure(1)
subplot(2, 1, 1)
plot(n, x)
title('Input signal')
subplot(2, 1, 2)
plot(n, y2)
title('Output of filter 2')

## Compare magnitude responses

Do y1 and y2 differ because filter 1 and 2 have different frequency response magnitudes? No, both filters have the same frequency response magnitude.

figure(1)
clf
plot(f, abs(H1), '-', f, abs(H2), '--')
legend('Filter 1', 'Filter 2')
xlabel('Frequency')
title('Magnitude responses')

## Compare phase responses

Do the filters have different phase responses?

figure(1)
clf
plot(f, angle(H1), f, angle(H2), '--')
legend('Filter 1', 'Filter 2')
title('Phase responses')
xlabel('Frequency')

% Yes, the phase responses are different.
% Filter 1 has linear phase (it delays all frequencies by the same amount).
% Filter 2 has nonlinear phase (it delays different frequencies by
% different amounts).

r = x + noise;                  % Add noise to input signal

figure(1)
clf
subplot(2, 1, 1)
plot(r)
title('Noisy input signal')

## Filter the noisy signal

Filter 1 removes the noise and preserves the signal waveform. Filter 2 removes the noise but DISTORTS the signal waveform.

y1 = filter(b1, a1, r);
y2 = filter(b2, a2, r);

figure(1)
clf
subplot(3, 1, 1)
plot(x)
title('Original signal')
subplot(3, 1, 2)
plot(y1)
title('Output of filter 1')
subplot(3, 1, 3)
plot(y2)
title('Output of filter 2')

## Impulse responses

Filter 1 has a symmetric impulse response (it has linear phase). Filter 2 has a non-symmetric impulse response (it does not have linear phase).

imp = [1 zeros(1, 50)];
h1 = filter(b1, a1, imp);
h2 = filter(b2, a2, imp);

figure(1)
clf
subplot(2, 1, 1)
stem(0:50, h1, '.')
title('Impulse response - filter 1')

subplot(2, 1, 2)
stem(0:50, h2, '.')
title('Impulse response - filter 2')

## Conclusion

The linear phase filter (filter 1) is preferable because it removes the noise without changing (distorting) the waveform.