Sampling in Matlab and downsampling an audio file

Generating a continuous signal and sampling it at a given rate is demonstrated here. In simulations, we may require to generate a continuous time signal and convert it to discrete domain by appropriate sampling.

For baseband signal, the sampling is straight forward. By Nyquist Shannon sampling theorem, for faithful reproduction of a continuous signal in discrete domain, one has to sample the signal at a rate higher than at-least twice the maximum frequency contained in the signal (actually, it is twice the one-sided bandwidth occupied by a real signal. For a baseband signal bandwidth ( to ) and maximum frequency in a given band are equivalent).

Matlab or any other simulation softwares  process everything in digital i.e, discrete in time. This is because, the signals are represented as discrete samples in computer memory. Therefore, we cannot generate a real continuous-time signal on it, rather we can generate a “continuous-like” signal by using a very very high sampling rate. When plotted, such signals look like a continuous signal.

Let’s generate a simple continuous-like sinusoidal signal with frequency . In order to make it appear as a continuous signal when plotting, a sampling rate of is used.

fs=500e3; %Very high sampling rate 500 kHz
f=10e3; %Frequency of sinusoid
nCyl=5; %generate five cycles of sinusoid
t=0:1/fs:nCyl*1/f; %time index
x=cos(2*pi*f*t);

plot(t,x)
title('Continuous sinusoidal signal');
xlabel('Time(s)');
ylabel('Amplitude');
Continuous time signal in Matlab
Figure 1: Continuous time signal in Matlab

Pretending the above generated signal as a “continuous” signal, we would like to convert the signal to discrete-time equivalent by sampling. By Nyquist Shannon Theorem, the signal has to be sampled at at-least . Let’s sample the signal at and then at  for illustration.

fs1=30e3; %30kHz sampling rate
t1=0:1/fs1:nCyl*1/f; %time index
x1=cos(2*pi*f*t1);

fs2=50e3; %50kHz sampling rate
t2=0:1/fs2:nCyl*1/f; %time index
x2=cos(2*pi*f*t2);

subplot(2,1,1);
plot(t,x);
hold on;
stem(t1,x1);
subplot(2,1,2);
plot(t,x);
hold on;
stem(t2,x2);
Figure 2: Sampling a Continuous time signal in Matlab

Manipulating audio files in Matlab

Matlab’s standard installation comes with a set of audio files. The audio files,that can be considered as one-dimensional vectors, can be inspected and played using xpsound command. With this command, we can visualize the audio files in three ways

● Time series (data-vector as function of time)
● Power spectral density (distribution of frequency content)
● Spectrogram (frequency content as function of time)

The output of the xpsound command plotting time-series plot of a sample audio file looks like this

Figure 3: Sound visualizer – xpsound

We can also load and plot the time-series plot using inbuilt Matlab commands as follows

>> load('gong') %load the variables for the 'gong' audio file, this loads the sample frequency and the sample values
>> Fs  %sampling frequency

Fs =

        8192

>> y(1:10) %first 10 sample values in the file

ans =

   -0.0027
   -0.0045
   -0.0074
   -0.0110
   -0.0128
   -0.0173
   -0.0223
   -0.0223
   -0.0200
   -0.0092
>> t=0:1/Fs:length(y)/Fs-1/Fs; %time index
>> figure;plot(t,y);xlabel('Time (s)'),ylabel('y')

Example: DownSampling

In signal processing, downsampling is the process of throwing away samples without applying any low-pass filtering. Mathematically, downsampling by a factor of implies, starting from the very first sample we throw away every $M-1$ samples (i.e, keep every -th sample.

For example, if is a vector of input samples, downsampling by implies

Going back to the previous example of ‘gong’ audio vector loaded in the Matlab variable space, the downsampling operation can be coded as follows.

>>M=2 % downsample by 2
>>y_down = y(1:M:end); % keep every M-th sample

Note: Downsampling↗ is not same as decimation. Decimation↗ implies reducing the sampling rate of a signal by applying low-pass filtering as the first step and then followed by decimation.

Rate this article: Note: There is a rating embedded within this post, please visit this post to rate it.

Reference

[1] Julius O smith III, “Spectral audio signal processing”, Center for Computer Research in Music and Acoustics, Stanford.↗

Books by the author


Wireless Communication Systems in Matlab
Second Edition(PDF)

Note: There is a rating embedded within this post, please visit this post to rate it.
Checkout Added to cart

Digital Modulations using Python
(PDF ebook)

Note: There is a rating embedded within this post, please visit this post to rate it.
Checkout Added to cart

Digital Modulations using Matlab
(PDF ebook)

Note: There is a rating embedded within this post, please visit this post to rate it.
Checkout Added to cart
Hand-picked Best books on Communication Engineering
Best books on Signal Processing

Sampling Theorem – Bandpass or Intermediate or Under Sampling

Prerequisite: Sampling theorem – baseband sampling

Intermediate Sampling or Under-Sampling

A signal is a bandpass signal if we can fit all its frequency content inside a bandwidth . Bandwidth is simply the difference between the lowest and the highest frequency present in the signal.

“In order for a faithful reproduction and reconstruction of a bandpass analog signal with bandwidth – , the signal should be sampled at a Sampling frequency () that is greater than or equal to twice the maximum bandwidth of the signal:

Consider a bandpass signal extending from 150Hz to 200Hz. The bandwidth of this signal is . In order to faithfully represent the above signal in the digital domain the sampling frequency must be . Note that the sampling frequency 100Hz is far below the maximum content of the signal (which is 200Hz). That is why the bandpass sampling is also called “under-sampling”. As long as the sampling frequency is greater than or equal to twice the bandwidth of the signal, the reconstruction back to analog domain will be error free.

Going back to the aliasing zone figure, if the signal of interest is in the zone other than zone 1, it is called a bandpass signal and the sampling operation is called “Intermediate Sampling” or “Harmonic Sampling” or “Under Sampling” or “Bandpass Sampling”.

Folding Frequencies and Aliasing Zones

Note that zone 1 is a mirror image of zone 2 (with frequency reversal). Similarly zone 3 is a mirror image of zone 4 etc.., Also, any signal in zone 1 will be reflected in zone 2 with frequency reversal which inturn will be copied in zone 3 and so on.

Lets say the signal of interest lies in zone 2. This will be copied in all the other zones. Zone 1 also contains the sampled signal with frequency reversal which can be correct by reversing the order of FFT in digital domain.

No matter in which zone the signal of interest lies, zone 1 always contains the signal after sampling operation is performed. If the signal of interest lies in any of the even zones, zone 1 contains the sampled signal with frequency reversal. If the signal of interest lies in any of the odd zones, zone 1 contains the sampled signal without frequency reversal.

Example:

Consider an AM signal centered at carrier frequency 1MHz, with two components offset by 10KHz – 0.99 MHz and 1.01 MHz. So the AM signal contains three frequency components at 0.99 MHz, 1 MHz and 1.01 MHz.

Our desire is to sample the AM signal. As with the usual sampling theorem (baseband), we know that if we sample the signal at twice the maximum frequency i.e Fs>=2*1.01MHz=2.02 MHz there should be no problem in representing the analog signal in digital domain.

By the bandpass sampling theorem, we do not need to use a sampler running at Fs>=2.02 MHz. Faster sampler implies more cost. By applying the bandpass sampling theorem, we can use a slower sampler and reduce the cost of the system. The bandwidth of the signal is 1.01MHz-0.99 MHz = 20 KHz. So, just sampling at will convert the signal to digital domain properly and we can also avoid using an expensive high rate sampler (if used according to baseband sampling theorem).

Lets set the sampling frequency to be (which is 3 times higher than the minimum required sampling rate of 40KHz or oversampling rate =3).

Now we can easily find the position of the spectral components in the sampled output by using the aliasing zone figure as given above. Since , will be 60KHz. So the zone 1 will be from 0 to 60 KHz, zone 2 -> 60-120KHz and so on. The three spectral components at 0.99MHz, 1MHz and 1.01 MHz will fall at zone 17 ( how ? 0.99 MHz/60 KHz = 16.5 , 1MHz/60KHz = 16.67 and 1.01MHz/60KHz = 16.83 , all figures approximating to 17). By the aliasing zone figure, zone 16 contains a copy of zone 17, zone 15 contains a copy of zone 16, zone 14 contains a copy of zone 15 and so on… Finally zone 1 contains the copy of zone 2 (Frequency reversal also exist at even zones). In effect, zone 1 contains a copy of zone 17. Since the original spectral components are at zone 17, which is an odd zone, zone 1 contains the copy of spectral components at zone 17 without frequency reversal.

Since there is no frequency reversal, in zone 1 the three components will be at 30KHz, 40KHz and 50KHz (You can easily figure this out ).

This operation has downconverted our signal from zone 17 to zone 1 without distorting the signal components. The downconverted signal can be further processed by using a filter to select the baseband downconverted components. Following figure illustrates the concept of bandpass sampling.

Bandpass Sampling

Consider the same AM signal with three components at 0.99MHz, 1MHz and 1.01 MHz. Now we also have an “unwanted” fourth component at 1.2 MHz along with the incoming signal. If we sample the signal at 120 KHz, it will cause aliasing (because the bandwidth of the entire signal is 1.2-0.99 = 0.21 MHz = 210 KHz and the sampling frequency of 120KHz is below twice the bandwidth). In order to avoid anti-aliasing and to discard the unwanted component at 1.2 MHz, an anti-aliasing bandpass filter has to be used to select those desired component before performing the sampling operation at 120KHz. This is also called “pre-filtering”. The following figure illustrates this concept.

Bandpass sampling with pre-filtering

See also:

[1] Oversampling, ADC – DAC Conversion,pulse shaping and Matched Filter
[2] Sampling Theorem Basics and Baseband Sampling