dow convert a signal in matlab

To convert a continuous analog signal to a digital signal in MATLAB, you need to perform sampling and quantization.

Sampling

Sampling is the process of converting a continuous signal into a discrete signal by measuring the amplitude of the signal at regular intervals of time. The sampling frequency is the number of samples per second and is expressed in Hertz (Hz).

main.m
% Sampling frequency and time interval 
fs = 1000; % 1000 Hz
Ts = 1/fs; % 0.001 seconds
88 chars
4 lines

Analog to Digital Conversion (ADC)

After sampling, the amplitude of the signal needs to be quantized into a finite number of levels. The number of levels determines the resolution and is expressed in bits. The quantization error is the difference between the continuous original signal and the quantized signal.

main.m
% Quantization parameters
nbits = 8; % 8-bit quantization
nlevels = 2^nbits; % number of levels
vmax = 5; % maximum voltage of the signal
vmin = -vmax; % minimum voltage of the signal
del = (vmax-vmin)/nlevels; % quantization step size
236 chars
7 lines

Example: Sampling and Quantization

main.m
% Generate continuous analog signal
t = 0:Ts:1-Ts; % time vector
f = 10; % frequency of the signal
x = sin(2*pi*f*t); % signal

% Sampling
xn = x(1:fs/f:end); % sampled signal

% Quantization
xq = round(xn/del)*del; % quantized signal

% Plot original, sampled and quantized signals
subplot(3,1,1); plot(t,x); title('Original signal');
subplot(3,1,2); plot(t,xn); title('Sampled signal');
subplot(3,1,3); plot(t,xq); title('Quantized signal');
444 chars
16 lines

This code will generate a plot with the original, sampled and quantized signal.

gistlibby LogSnag