Kahibaro
Discord Login Register

Basic Signal Representation in MATLAB

Understanding Signals in MATLAB

In MATLAB a signal is usually represented as an array of numbers that vary with time or with some other independent variable. For beginners, it is enough to think of a signal as a vector that holds sampled values of a quantity, such as sound amplitude, sensor voltage, or image intensity along a line.

The most common representation for a one dimensional signal in MATLAB is a row or column vector of real numbers. For example, a short synthetic signal might be stored in a variable x as [0 1 0 -1]. MATLAB does not force you to use a particular orientation, but in many signal processing workflows a signal is stored as a column vector with size $N \times 1$, where $N$ is the number of samples. The type is usually double, which is MATLAB’s default numeric type, and is sufficient for most introductory signal work.

You should distinguish clearly between the signal values and the independent variable, often time, that those values correspond to. MATLAB does not automatically know the sampling times or the sampling frequency. You must represent these explicitly if you need them.

Sampled Signals and Discrete Time

In practice MATLAB works with discrete time signals, that is signals defined only at specific sample points. A continuous time signal, such as an ideal sine wave defined for all real times $t$, must be sampled to be used in MATLAB. You choose a sampling period $T_s$ or a sampling frequency $f_s$, then evaluate the continuous signal at those discrete times.

If $x(t)$ is a continuous time signal and $T_s$ is the sampling period, the discrete time signal is

$$x[n] = x(n T_s), \quad n = 0, 1, 2, \dots, N-1.$$

In MATLAB you represent the discrete time index $n$ with an integer vector, and the time axis with a real vector. A common style is

N  = 1000;         % number of samples
fs = 1000;         % sampling frequency in Hz
Ts = 1/fs;         % sampling period in seconds
n  = 0:N-1;        % discrete time index
t  = n*Ts;         % time vector in seconds
x  = sin(2*pi*50*t);

Here x is the discrete signal. The vector t stores the time associated with each sample, while n stores the sample index. MATLAB does not enforce any link between x and t, you maintain that relationship through consistent vector lengths and indexing.

Time Vectors and Sampling Frequency

For many tasks you should create a time vector explicitly. This makes it easier to plot, to compute durations, and to reason about frequencies. The time vector contains one entry per sample. If the signal has $N$ samples and the sampling frequency is $f_s$, then the total duration is

$$T = \frac{N-1}{f_s}.$$

To construct the time vector, you use either the integer index multiplied by the sampling period, as shown before, or you use a start, step, and end syntax similar to

t = 0:Ts:(N-1)*Ts;

The important detail is that the length of t must match the length of the signal vector. MATLAB will then align t and x correctly in operations like plotting. If you forget to define the time axis and only work with indices, you can still process the signal, but you lose physical units like seconds or Hertz.

When you already know the desired duration $T$ and the sampling frequency $f_s$, you can construct both at once. For example, if you want a one second long signal sampled at $f_s = 8000$ Hz you can write

fs = 8000;
T  = 1;
t  = 0:1/fs:T-1/fs;
x  = sin(2*pi*440*t);

Now x has $8000$ samples and represents a 440 Hz tone over one second.

Common Synthetic Signals

To get used to signal representation in MATLAB it helps to create some simple synthetic signals. The most basic examples are constant signals, ramps, and simple periodic functions.

A constant signal has the same value at all times. If you want $N$ samples with value $c$, you can use

N = 100;
c = 3;
x = c*ones(N,1);

A ramp signal increases linearly with the sample index. If $x[n] = a n + b$, you can write

N = 100;
n = (0:N-1).';
a = 0.1;
b = 1.0;
x = a*n + b;

Periodic signals such as sine and cosine waves are especially common. A discrete sine wave with amplitude $A$, frequency $f_0$ (in Hertz), sampling frequency $f_s$, and phase $\phi$ can be expressed as

$$x[n] = A \sin\bigl(2\pi f_0 n / f_s + \phi\bigr).$$

In MATLAB form you typically write

fs   = 1000;          % Hz
N    = 1000;
n    = (0:N-1).';
f0   = 50;            % Hz
A    = 2;
phi  = pi/4;          % radians
x = A*sin(2*pi*f0*n/fs + phi);

This pattern of using the discrete time index, the sampling frequency, and trigonometric functions appears in many signal processing tasks in MATLAB.

Real and Complex Valued Signals

Many introductory signals are real valued. MATLAB also handles complex valued signals directly. A complex signal has a real part and an imaginary part at each sample. You create such signals using the imaginary unit 1i or 1j in MATLAB.

A typical complex exponential signal is

$$x[n] = e^{j 2\pi f_0 n / f_s} = \cos\bigl(2\pi f_0 n / f_s\bigr) + j \sin\bigl(2\pi f_0 n / f_s\bigr).$$

In MATLAB you can write

fs = 1000;
N  = 100;
n  = (0:N-1).';
f0 = 100;
x  = exp(1i*2*pi*f0*n/fs);

Here x is a complex vector. MATLAB displays complex entries in the form a + b i. You can access the real part with real(x) and the imaginary part with imag(x). You can also compute the magnitude and phase with abs(x) and angle(x). Complex signal representation is particularly important in modulation, filtering, and frequency domain analysis, but for basic representation it is enough to know that MATLAB stores both parts in a single array.

Multi Channel and Vector Signals

In many applications you work with more than one signal at the same time, for example stereo audio or multiple sensors. MATLAB represents these multi channel signals as matrices, where one dimension is time and the other dimension is channel index.

A common convention is to store each channel in a column, with rows representing time samples. If there are $N$ samples and $M$ channels, the matrix has size $N \times M$. For instance, a stereo signal has two channels and can be stored as

N      = 1000;
fs     = 44100;
t      = (0:N-1).'/fs;
left   = sin(2*pi*440*t);
right  = sin(2*pi*880*t);
x      = [left right];   % N-by-2 matrix

Here x(:,1) is the left channel and x(:,2) is the right channel. Operations that treat each column as a separate signal are very natural in this arrangement, because many MATLAB functions operate columnwise by default.

You can also represent multiple signals stacked in rows, but this is less common for time series. If you adopt a consistent convention, you avoid confusion when calling functions that expect signals in specific shapes.

Amplitude Scaling and Units

The numerical values in a signal vector represent a physical or normalized amplitude. MATLAB itself does not attach units, but you should be aware of the meaning of the values. In audio processing, for instance, digital signals are frequently normalized so that the maximum absolute value is $1$. In that case a vector element x(k) = 0.5 might represent half of the maximum possible amplitude.

Scaling a signal is straightforward. If $y[n] = c x[n]$, then in MATLAB you simply write

c = 0.5;
y = c*x;

Offsetting the signal by a constant corresponds to adding a scalar to the entire vector. This is common when changing reference levels or introducing a bias.

Although MATLAB does not enforce amplitude units, it is useful to keep them in mind when you combine signals, mix channels, or compare to thresholds. You ensure consistency by tracking what each array represents and what any constant factors mean in your application.

Simple Time Shifts and Reversal

Basic time manipulations are easy to implement once you understand the discrete index representation. A time shift corresponds to an index shift, while time reversal corresponds to flipping the order of samples.

If $y[n] = x[n - n_0]$ is a delayed version of $x[n]$ by $n_0$ samples, you can create a delayed signal by padding with zeros at the beginning. For a delay of five samples you might write

n0 = 5;
y  = [zeros(n0,1); x];

In this example the length of y is greater than the length of x. When you work with fixed length vectors, you instead shift within that length and discard samples that move beyond the edges.

Time reversal is represented mathematically as

$$y[n] = x[N-1-n],$$

for $n = 0, 1, \dots, N-1$. MATLAB provides a function flipud for columns and fliplr for rows, or simply flip to reverse an array. For a column vector signal you can write

y = flip(x);

The relationship between these simple operations and their mathematical definitions is direct once you view the signal as a finite set of samples indexed from $0$ to $N-1$ or 1 to N in MATLAB’s one based indexing.

Basic Visual Inspection with Plots

Even without going deeply into visualization, it is useful to see how the representation of a signal relates to its appearance. When you have a time vector t and a signal x, you can create a simple plot with

Views: 3

Comments

Please login to add a comment.

Don't have an account? Register now!