# Simulation of Aliasing

Started by July 6, 2020
```I ran across a problem in a DSP book that got me to thinking (the best kind=
of problem).  The author suggests that we can illustrate aliasing by sampl=
ing an audio signal at a high frequency and then replacing samples with zer=
oes to get a lower effective sampling frequency.  For example, if we sample=
at fs1 =3D 32 kHz and then keep every M =3D 4th sample (replacing the othe=
r samples with zeroes) then the resulting signal is equivalent to sampling =
at fs2 =3D 8 kHz (fs1/M).

I agree that aliasing is present (if the original signal has content above =
4 kHz), but if you play the new signal back at 32 kHz you will also hear "r=
eplica" distortion.  To get a better sense of just aliasing due to sampling=
at 8 kHz you should low pass filter the new signal with a cutoff frequency=
of 4 kHz (fs2/2 or fs1/(2M)).

The process appears to be the same as down-sampling (without an anti-aliasi=
ng filter, since the goal is to demonstrate aliasing) followed by up-sampli=
ng at the same rate.  I think you still need to follow the up-sampler with =
a low-pass (replica elimination) filter to generate a signal that when play=
ed back at 32 kHz sounds the same as sampling (and replaying) the original =
analog signal at 8 kHz (without using an anti-aliasing filter).

Note: I am not arguing that the author is wrong.  I am just looking for ver=
ification that my thinking is correct (or not). The problem is presented ve=
ry early in the book (and it is a very good book) before filtering is even =
discussed.  He says that this zero-replacement method will illustrate disto=
rtion due to aliasing.  He does not say that it is exactly equivalent to th=
e signal that would be obtained by sampling at the lower rate without an an=
ti-aliasing filter.  It just got me to thinking about a better way (perhaps=
) to demonstrate only aliasing.

Tony
```
```On Monday, July 6, 2020 at 8:04:23 AM UTC-7, richard...@gmail.com wrote:
> I ran across a problem in a DSP book that got me to thinking
> (the best kind of problem).
> The author suggests that we can illustrate aliasing by sampling an
> audio signal at a high frequency and then replacing samples with zeroes
> to get a lower effective sampling frequency.  For example, if we
> sample at fs1 = 32 kHz and then keep every M = 4th sample
> (replacing the other samples with zeroes) then the resulting signal
> is equivalent to sampling at fs2 = 8 kHz (fs1/M).

> I agree that aliasing is present (if the original signal has content
> above 4 kHz), but if you play the new signal back at 32 kHz you will
> also hear "replica" distortion.  To get a better sense of just aliasing
> due to sampling at 8 kHz you should low pass filter the new signal
> with a cutoff frequency of 4 kHz (fs2/2 or fs1/(2M)).

Without getting too much detail, it does seem like two different things.

I didn't know the name "replica distortion" before.

Another choice is to generate four copies of every fourth sample.

I think you should try them all and see what they sound like.
```
```On Monday, July 6, 2020 at 12:30:41 PM UTC-5, ga...@u.washington.edu wrote:
> On Monday, July 6, 2020 at 8:04:23 AM UTC-7, richard...@gmail.com wrote:
> > I ran across a problem in a DSP book that got me to thinking
> > (the best kind of problem).=20
> > The author suggests that we can illustrate aliasing by sampling an
> > audio signal at a high frequency and then replacing samples with zeroes
> > to get a lower effective sampling frequency.  For example, if we
> > sample at fs1 =3D 32 kHz and then keep every M =3D 4th sample
> > (replacing the other samples with zeroes) then the resulting signal
> > is equivalent to sampling at fs2 =3D 8 kHz (fs1/M).
>=20
> > I agree that aliasing is present (if the original signal has content
> > above 4 kHz), but if you play the new signal back at 32 kHz you will
> > also hear "replica" distortion.  To get a better sense of just aliasing
> > due to sampling at 8 kHz you should low pass filter the new signal
> > with a cutoff frequency of 4 kHz (fs2/2 or fs1/(2M)).
>=20
> Without getting too much detail, it does seem like two different things.
>=20
> I didn't know the name "replica distortion" before.
>=20
> Another choice is to generate four copies of every fourth sample.
>=20
> I think you should try them all and see what they sound like.

Oh, I have tried it.  With a 400 Hz tone and original sampling at 32 kHz an=
d then reducing the sample rate to 2 kHz you can hear the 1600 Hz replica. =
There are spectral lines at (n*2000 +- 400).  If you reduce the sample rate=
to 500 Hz you can hear an alias at 100 Hz. The spectral lines are at (n*50=
0 +- 400). I am playing around with a replica elimination filter now so tha=
t I only hear the distortion due to aliasing. The goal is to produce a sign=
al that would sound the same as an undersampled signal.

```
```On Monday, July 6, 2020 at 1:03:21 PM UTC-5, richard...@gmail.com wrote:
> On Monday, July 6, 2020 at 12:30:41 PM UTC-5, ga...@u.washington.edu wrote:
> > On Monday, July 6, 2020 at 8:04:23 AM UTC-7, richard...@gmail.com wrote:
> > > I ran across a problem in a DSP book that got me to thinking
> > > (the best kind of problem).
> > > The author suggests that we can illustrate aliasing by sampling an
> > > audio signal at a high frequency and then replacing samples with zeroes
> > > to get a lower effective sampling frequency.  For example, if we
> > > sample at fs1 = 32 kHz and then keep every M = 4th sample
> > > (replacing the other samples with zeroes) then the resulting signal
> > > is equivalent to sampling at fs2 = 8 kHz (fs1/M).
> >
> > > I agree that aliasing is present (if the original signal has content
> > > above 4 kHz), but if you play the new signal back at 32 kHz you will
> > > also hear "replica" distortion.  To get a better sense of just aliasing
> > > due to sampling at 8 kHz you should low pass filter the new signal
> > > with a cutoff frequency of 4 kHz (fs2/2 or fs1/(2M)).
> >
> > Without getting too much detail, it does seem like two different things.
> >
> > I didn't know the name "replica distortion" before.
> >
> > Another choice is to generate four copies of every fourth sample.
> >
> > I think you should try them all and see what they sound like.
>
> Oh, I have tried it.  With a 400 Hz tone and original sampling at 32 kHz
> and then reducing the sample rate to 2 kHz you can hear the 1600 Hz
> replica. There are spectral lines at (n*2000 +- 400).  If you reduce the
> sample rate to 500 Hz you can hear an alias at 100 Hz. The spectral lines
> are at (n*500 +- 400). I am playing around with a replica elimination
> filter now so that I only hear the distortion due to aliasing. The goal is
> to produce a signal that would sound the same as an undersampled signal.

The Octave/MATLAB code for this is below:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This procedure is equivalent to down-sampling (without a LPF anti-aliasing
% filter) followed by up-sampling (without a LPF anti-replica) filter.
%
% To truly hear only aliasing a LPF anti-replica filter should be used.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

f0 = 400;
fs = 32000; % sample rate
t = 2; % seconds

xf = @(f0, fs, n) 0.75*sin(2*pi*f0*n/fs);
n = (0:t*fs-1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x = xf(f0, fs, n);
X = fft(x);
clf;
subplot(3,2,1)
plot((0:length(X)-1)*fs/length(X), 20*log10(abs(X)))
xlim([0 5000])
drawnow();
player = audioplayer(x, fs);
playblocking(player)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fsn = 2000;
r = fs/fsn;  % 16
xn = zeros(size(x));
xn(1:r:end) = x(1:r:end);
X = fft(xn);
subplot(3,2,3)
plot((0:length(X)-1)*fs/length(X), 20*log10(abs(X)))
xlim([0 5000])
drawnow();
player = audioplayer(xn, fs);
playblocking(player)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fsn = 500;
r = fs/fsn;  % 64
xn = zeros(size(x));
xn(1:r:end) = x(1:r:end);
X = fft(xn);
subplot(3,2,5)
plot((0:length(X)-1)*fs/length(X), 20*log10(abs(X)))
xlim([0 5000])
drawnow();
player = audioplayer(xn, fs);
playblocking(player)

```
```On Monday, July 6, 2020 at 12:30:41 PM UTC-5, ga...@u.washington.edu wrote:
> On Monday, July 6, 2020 at 8:04:23 AM UTC-7, richard...@gmail.com wrote:
> > I ran across a problem in a DSP book that got me to thinking
> > (the best kind of problem).=20
> > The author suggests that we can illustrate aliasing by sampling an
> > audio signal at a high frequency and then replacing samples with zeroes
> > to get a lower effective sampling frequency.  For example, if we
> > sample at fs1 =3D 32 kHz and then keep every M =3D 4th sample
> > (replacing the other samples with zeroes) then the resulting signal
> > is equivalent to sampling at fs2 =3D 8 kHz (fs1/M).
>=20
> > I agree that aliasing is present (if the original signal has content
> > above 4 kHz), but if you play the new signal back at 32 kHz you will
> > also hear "replica" distortion.  To get a better sense of just aliasing
> > due to sampling at 8 kHz you should low pass filter the new signal
> > with a cutoff frequency of 4 kHz (fs2/2 or fs1/(2M)).
>=20
> Without getting too much detail, it does seem like two different things.
>=20
> I didn't know the name "replica distortion" before.
>=20
> Another choice is to generate four copies of every fourth sample.
>=20
> I think you should try them all and see what they sound like.

Generating four copies of every fourth sample would be equivalent to convol=
ving the zeroed signal with a 4-sample rect pulse.  in the frequency domain=
this would be equivalent to multiplying by a sinc whose first null is at t=
he location of the first replica.  I don't see this is being as effective i=
n achieving my goal as a LP replica elimination filter.  (The goal is to pr=
oduce a signal which has the same spectrum as undersampling the original an=
alog signal.)
```