KAHIBARO Bookshelves
Discord Login Register

18.1. 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: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Getting Started with MATLAB
    • ☰ 1.1. What Is MATLAB and What Is It Used For
      • ☰ 1.2. Installing MATLAB and Setting Up the Environment
        • ☰ 1.3. Understanding the MATLAB Desktop Interface
          • ☰ 1.4. Command Window, Workspace, and Current Folder
            • ☰ 1.5. Basic Workflow and Running Simple Commands
              • ☰ 1.6. Getting Help and Documentation
              • ☰ 2. MATLAB Language Basics
                • ☰ 2.1. MATLAB Syntax and Expressions
                  • ☰ 2.2. Variables and Assignment
                    • ☰ 2.3. Data Types and Type Conversion
                      • ☰ 2.4. Vectors and Matrices Fundamentals
                        • ☰ 2.5. Creating and Indexing Arrays
                          • ☰ 2.6. Basic Arithmetic and Elementwise Operations
                            • ☰ 2.7. Using Built-In Constants and Functions
                            • ☰ 3. Working with Variables and the Workspace
                              • ☰ 3.1. Creating, Viewing, and Editing Variables
                                • ☰ 3.2. Workspace Browser and Variable Inspector
                                  • ☰ 3.3. Saving and Loading Data Files
                                    • ☰ 3.4. MAT-Files and Importing External Data
                                      • ☰ 3.5. Clearing Variables and Managing the Workspace
                                      • ☰ 4. Vectors, Matrices, and Arrays
                                        • ☰ 4.1. Row and Column Vectors
                                          • ☰ 4.2. Matrix Creation and Concatenation
                                            • ☰ 4.3. Indexing, Slicing, and Logical Indexing
                                              • ☰ 4.4. Matrix Operations vs Elementwise Operations
                                                • ☰ 4.5. Reshaping and Rearranging Arrays
                                                  • ☰ 4.6. Special Matrices and Functions
                                                  • ☰ 5. Basic Programming Constructs
                                                    • ☰ 5.1. Scripts vs Functions
                                                      • ☰ 5.2. Control Flow with if, elseif, else
                                                        • ☰ 5.3. Loops with for and while
                                                          • ☰ 5.4. break, continue, and return
                                                            • ☰ 5.5. Logical Expressions and Relational Operators
                                                              • ☰ 5.6. Best Practices for Clear and Readable Code
                                                              • ☰ 6. Functions in MATLAB
                                                                • ☰ 6.1. Creating Simple Functions
                                                                  • ☰ 6.2. Function Inputs and Outputs
                                                                    • ☰ 6.3. Function Files and the Path
                                                                      • ☰ 6.4. Local vs Global Variables
                                                                        • ☰ 6.5. Anonymous Functions
                                                                          • ☰ 6.6. Organizing Code into Multiple Functions
                                                                          • ☰ 7. Working with Numbers and Math
                                                                            • ☰ 7.1. Scalars and Vectorized Computations
                                                                              • ☰ 7.2. Basic Mathematical Functions
                                                                                • ☰ 7.3. Trigonometric and Exponential Functions
                                                                                  • ☰ 7.4. Complex Numbers in MATLAB
                                                                                    • ☰ 7.5. Rounding, Modulo, and Special Values
                                                                                      • ☰ 7.6. Numerical Precision and Floating Point Considerations
                                                                                      • ☰ 8. Strings, Characters, and Categorical Data
                                                                                        • ☰ 8.1. Character Arrays and String Arrays
                                                                                          • ☰ 8.2. Creating and Manipulating Strings
                                                                                            • ☰ 8.3. Converting Between Data Types and Strings
                                                                                              • ☰ 8.4. Comparing and Searching Strings
                                                                                                • ☰ 8.5. Categorical Arrays Basics
                                                                                                • ☰ 9. Tables, Structures, and Cell Arrays
                                                                                                  • ☰ 9.1. Introduction to Tables
                                                                                                    • ☰ 9.2. Creating Tables and Accessing Data
                                                                                                      • ☰ 9.3. Modifying and Joining Tables
                                                                                                        • ☰ 9.4. Structures and Nested Structures
                                                                                                          • ☰ 9.5. Cell Arrays and Cell Indexing
                                                                                                            • ☰ 9.6. Choosing the Right Container Type
                                                                                                            • ☰ 10. Importing and Exporting Data
                                                                                                              • ☰ 10.1. Reading Text and CSV Files
                                                                                                                • ☰ 10.2. Importing Excel Spreadsheets
                                                                                                                  • ☰ 10.3. Importing Data with the Import Tool
                                                                                                                    • ☰ 10.4. Exporting Data to Files
                                                                                                                      • ☰ 10.5. Working with Image and Audio Files
                                                                                                                        • ☰ 10.6. Basic File Management from MATLAB
                                                                                                                        • ☰ 11. Plotting and Visualization
                                                                                                                          • ☰ 11.1. Introduction to 2D Plotting
                                                                                                                            • ☰ 11.2. Customizing Plots and Axes
                                                                                                                              • ☰ 11.3. Multiple Plots in One Figure
                                                                                                                                • ☰ 11.4. Labels, Titles, Legends, and Annotations
                                                                                                                                  • ☰ 11.5. Plotting with Different Line and Marker Styles
                                                                                                                                    • ☰ 11.6. Saving and Exporting Figures
                                                                                                                                    • ☰ 12. Advanced Plotting for Beginners
                                                                                                                                      • ☰ 12.1. Subplots and Tiled Layouts
                                                                                                                                        • ☰ 12.2. Bar, Scatter, and Stem Plots
                                                                                                                                          • ☰ 12.3. Histograms and Density Plots
                                                                                                                                            • ☰ 12.4. 3D Plots and Surface Plots
                                                                                                                                              • ☰ 12.5. Using plottools and Interactive Editing
                                                                                                                                                • ☰ 12.6. Basics of Handle Graphics
                                                                                                                                                • ☰ 13. Working with Scripts and Live Scripts
                                                                                                                                                  • ☰ 13.1. Creating and Running Script Files
                                                                                                                                                    • ☰ 13.2. Sectioning Code and Running Sections
                                                                                                                                                      • ☰ 13.3. Live Scripts and Rich Text
                                                                                                                                                        • ☰ 13.4. Adding Equations, Images, and Outputs
                                                                                                                                                          • ☰ 13.5. Sharing and Exporting Live Scripts
                                                                                                                                                          • ☰ 14. Debugging and Code Quality
                                                                                                                                                            • ☰ 14.1. Finding and Fixing Syntax Errors
                                                                                                                                                              • ☰ 14.2. Setting Breakpoints and Stepping Through Code
                                                                                                                                                                • ☰ 14.3. Inspecting Variable Values During Execution
                                                                                                                                                                  • ☰ 14.4. Using the Editor Debugging Tools
                                                                                                                                                                    • ☰ 14.5. Using Code Analyzer and Warnings
                                                                                                                                                                      • ☰ 14.6. Writing Clean and Maintainable MATLAB Code
                                                                                                                                                                      • ☰ 15. Basic Linear Algebra with MATLAB
                                                                                                                                                                        • ☰ 15.1. Vectors and Norms
                                                                                                                                                                          • ☰ 15.2. Matrix Addition and Multiplication
                                                                                                                                                                            • ☰ 15.3. Solving Linear Systems with Backslash
                                                                                                                                                                              • ☰ 15.4. Inverses, Determinants, and Rank
                                                                                                                                                                                • ☰ 15.5. Eigenvalues and Eigenvectors
                                                                                                                                                                                  • ☰ 15.6. Using Linear Algebra Functions Safely
                                                                                                                                                                                  • ☰ 16. Data Analysis and Statistics Fundamentals
                                                                                                                                                                                    • ☰ 16.1. Descriptive Statistics and Summary Measures
                                                                                                                                                                                      • ☰ 16.2. Sorting, Filtering, and Grouping Data
                                                                                                                                                                                        • ☰ 16.3. Basic Correlation and Regression
                                                                                                                                                                                          • ☰ 16.4. Simple Data Cleaning Techniques
                                                                                                                                                                                            • ☰ 16.5. Basic Time Series Handling
                                                                                                                                                                                            • ☰ 17. Working with Time and Dates
                                                                                                                                                                                              • ☰ 17.1. Representing Dates and Times
                                                                                                                                                                                                • ☰ 17.2. Creating and Formatting datetime Arrays
                                                                                                                                                                                                  • ☰ 17.3. Date Calculations and Differences
                                                                                                                                                                                                    • ☰ 17.4. Plotting Data over Time
                                                                                                                                                                                                    • ☰ 18. Introduction to Signal and Image Handling
                                                                                                                                                                                                      • ☰ 18.1. Basic Signal Representation in MATLAB
                                                                                                                                                                                                        • ☰ 18.2. Simple Filtering and Transformations
                                                                                                                                                                                                          • ☰ 18.3. Reading, Displaying, and Writing Images
                                                                                                                                                                                                            • ☰ 18.4. Basic Image Transformations
                                                                                                                                                                                                            • ☰ 19. Automation and Scripting Tasks
                                                                                                                                                                                                              • ☰ 19.1. Batch Processing of Files
                                                                                                                                                                                                                • ☰ 19.2. Writing Reusable Utility Scripts
                                                                                                                                                                                                                  • ☰ 19.3. Using Relative Paths and the MATLAB Path
                                                                                                                                                                                                                    • ☰ 19.4. Simple User Interaction with input and disp
                                                                                                                                                                                                                    • ☰ 20. Introduction to Toolboxes
                                                                                                                                                                                                                      • ☰ 20.1. What Are Toolboxes and How to Use Them
                                                                                                                                                                                                                        • ☰ 20.2. Exploring Installed Toolboxes
                                                                                                                                                                                                                          • ☰ 20.3. Finding Functions in Toolboxes
                                                                                                                                                                                                                            • ☰ 20.4. Add-Ons and MATLAB File Exchange
                                                                                                                                                                                                                            • ☰ 21. Best Practices and Next Steps
                                                                                                                                                                                                                              • ☰ 21.1. Organizing Projects and Folders
                                                                                                                                                                                                                                • ☰ 21.2. Version Control Basics with MATLAB
                                                                                                                                                                                                                                  • ☰ 21.3. Performance Tips for Beginners
                                                                                                                                                                                                                                    • ☰ 21.4. Learning Resources and Documentation Path
                                                                                                                                                                                                                                      • ☰ 21.5. Suggested Practice Projects for Beginners