MATLAB Syntax and Commands
Basic MATLAB Syntax and Commands
1. **Defining Vectors and Matrices**
```matlab
n = 0:9;
t = 0:0.001:10;
t_new = 0:1/fs_new:T;
```
- **Explanation**: Defines a vector `n` with values from 0 to 9. Defines a vector `t` with values from 0 to 10 in steps of
0.001. Defines a vector `t_new` with values from 0 to `T` (duration) in steps of `1/fs_new`.
2. **Generating Signals**
```matlab
y = heaviside(n);
y = A * pulstran(t,0:T:10,@rectpuls,dutyCycle*T/100);
y = A * pulstran(t,0:T:10,@tripuls,T/2);
y = sin(2*pi*f*n + beta*sin(2*pi*f_mod*n));
y = exp(a * n) .* exp(-damping_factor * n);
pulse = gauspuls(t, 5, 0.5);
```
- **Explanation**: Generates different signals: `heaviside` for unit step, `pulstran` with `rectpuls` for rectangular
signals, `tripuls` for triangle signals, sinusoidal signals with frequency modulation, damped exponential signals, and
MATLAB Syntax and Commands
`gauspuls` for Gaussian pulses.
3. **Plotting Signals**
```matlab
stem(n, y, 'filled');
plot(t, y);
```
- **Explanation**: `stem` creates a stem plot, and `plot` creates a 2D line plot. The `'filled'` option for `stem` fills the
markers.
4. **Labeling and Titling Plots**
```matlab
xlabel('Time (s)');
ylabel('Amplitude');
title('Unit Step Function');
grid on;
```
- **Explanation**: Adds labels to the x-axis and y-axis, sets the plot title, and turns on the grid.
5. **Fast Fourier Transform (FFT)**
```matlab
freq_domain = fftshift(fft(pulse));
```
MATLAB Syntax and Commands
- **Explanation**: Computes the FFT of `pulse` and shifts zero-frequency components to the center of the spectrum.
6. **Defining Frequencies**
```matlab
fs = 1/0.001;
f = linspace(-fs/2, fs/2, length(freq_domain));
```
- **Explanation**: Defines the sampling rate `fs` and creates a frequency vector `f`.
7. **Amplitude Modulation**
```matlab
s_t = (1 + m_t) .* c_t;
```
- **Explanation**: Modulates the carrier signal `c_t` with the message signal `m_t`.
8. **Hilbert Transform**
```matlab
analytic_signal = hilbert(s_t);
envelope = abs(analytic_signal);
```
- **Explanation**: Computes the analytic signal using the Hilbert transform and extracts its envelope.
MATLAB Syntax and Commands
9. **Phase Locked Loop (PLL)**
```matlab
pll = [Link]('NormalizedLoopBandwidth', 0.01, 'DampingFactor', 0.7, 'DetectorGain', 1);
demodulated_signal = pll(s_t');
```
- **Explanation**: Uses a Phase Locked Loop for FM demodulation with specified parameters.
10. **Interpolation**
```matlab
reconstructed_signal = interp1(t_new, sampled_signal, t, 'linear');
```
- **Explanation**: Reconstructs the signal using linear interpolation.
11. **Time Division Multiplexing (TDM)**
```matlab
multiplexed_signal = reshape(channels, 1, N*length(t));
demultiplexed_signals = reshape(multiplexed_signal, N, length(t));
```
- **Explanation**: Reshapes the matrix `channels` into a single row for multiplexing and back for demultiplexing.
12. **Non-uniform Quantization (mu-law)**
MATLAB Syntax and Commands
```matlab
x_quantized = sign(x_sampled) .* log(1 + mu * abs(x_sampled)) / log(1 + mu);
```
- **Explanation**: Applies mu-law compression for non-uniform quantization.
13. **Binary Encoding**
```matlab
x_encoded = de2bi(x_quantized_integer, bits, 'left-msb');
```
- **Explanation**: Converts decimal integers to binary vectors with specified bit-length using left-most significant bit
ordering.
14. **FFT Spectrum Calculation**
```matlab
M_f = fftshift(fft(m_t)) / length(m_t);
```
- **Explanation**: Computes and normalizes the FFT of the message signal.
MATLAB Syntax and Commands
Detailed Explanations of Selected Syntaxes
1. **heaviside(n)**
- Generates a unit step function.
2. **pulstran(t,d,func,width)**
- Generates a pulse train where `func` specifies the pulse shape and `width` specifies the pulse width.
3. **tripuls(t,width)**
- Generates a triangular pulse.
4. **gauspuls(t,fc,bw)**
- Generates a Gaussian-modulated sinusoidal pulse with center frequency `fc` and bandwidth `bw`.
5. **fftshift(x)**
- Shifts the zero-frequency component to the center of the spectrum.
6. **hilbert(x)**
- Computes the analytic signal using the Hilbert transform.
7. **[Link]**
- Creates a Phase Locked Loop (PLL) object for demodulating frequency-modulated signals.
8. **interp1(x,v,xq,method)**
MATLAB Syntax and Commands
- Interpolates the data `v` at the points `x` using the specified interpolation method (`linear` in this case).
9. **reshape(A,m,n)**
- Reshapes the matrix `A` into an `m`-by-`n` matrix.
10. **de2bi(d,n,msbflag)**
- Converts decimal integers to binary vectors with specified number of bits `n` and most significant bit order `msbflag`.
This comprehensive list covers the syntaxes and functions used in the MATLAB programs, providing both basic and
advanced signal processing techniques.
MATLAB Syntax and Commands
Basic MATLAB Syntax and Commands
1. **Defining Vectors and Matrices**
```matlab
n = 0:9;
t = 0:0.01:10;
t_new = 0:1/fs_new:T;
```
- **Explanation**: Defines a vector `n` with values from 0 to 9. Defines a vector `t` with values from 0 to 10 in steps of
0.01. Defines a vector `t_new` with values from 0 to `T` (duration) in steps of `1/fs_new`.
2. **Generating Signals**
```matlab
y = ones(1, 10);
y = A * square(2*pi/T * t, 30);
y = A * sawtooth(2*pi/T * t, 0.5);
y = sin(2*pi*f*n);
y = exp(a * n);
pulse = pulse_amplitude * (abs(t) <= pulse_width/2);
m_t = Am * sin(2*pi*fm*t);
c_t = Ac * sin(2*pi*fc*t);
s_t = (1 + m_t) .* c_t;
s_t = fmmod(m_t, fc, Fs, kf*Am);
MATLAB Syntax and Commands
low_pass_signal = sin(2)
- **Explanation**: Generates various signals including unit step, rectangular, triangular, sinusoidal, exponential,
rectangular pulse, message and carrier signals, amplitude modulated signal, frequency modulated signal, and low-pass
signal.
3. **Plotting Signals**
```matlab
figure;
stem(n, y, 'filled');
plot(t, y);
subplot(3,1,1);
```
- **Explanation**: `figure` creates a new figure window. `stem` creates a stem plot, `plot` creates a 2D line plot, and
`subplot` creates subplots within a figure window.
4. **Labeling and Titling Plots**
```matlab
xlabel('Time (s)');
ylabel('Amplitude');
title('Unit Step Function');
grid on;
```
- **Explanation**: Adds labels to the x-axis and y-axis, sets the plot title, and turns on the grid.
MATLAB Syntax and Commands
5. **Fast Fourier Transform (FFT)**
```matlab
freq_domain = fftshift(fft(pulse));
magnitude_spectrum = abs(freq_domain);
```
- **Explanation**: Computes the FFT of `pulse`, shifts zero-frequency components to the center of the spectrum, and
calculates the magnitude spectrum.
6. **Defining Frequencies**
```matlab
fs = 1/0.01;
f = linspace(-fs/2, fs/2, length(freq_domain));
```
- **Explanation**: Defines the sampling rate `fs` and creates a frequency vector `f`.
7. **Amplitude Modulation**
```matlab
s_t = (1 + m_t) .* c_t;
```
- **Explanation**: Modulates the carrier signal `c_t` with the message signal `m_t`.
MATLAB Syntax and Commands
8. **Hilbert Transform**
```matlab
s_env = abs(hilbert(s_t));
```
- **Explanation**: Computes the analytic signal using the Hilbert transform and extracts its envelope.
9. **Frequency Modulation and Demodulation**
```matlab
s_t = fmmod(m_t, fc, Fs, kf*Am);
demodulated_signal = fmdemod(s_t, fc, Fs, kf*Am);
```
- **Explanation**: Performs frequency modulation and demodulation using `fmmod` and `fmdemod` functions.
10. **Sampling and Reconstruction**
```matlab
x_sampled = sin(2*pi*f_signal*t_new);
reconstructed_signal = interp1(t_new, sampled_signal, t, 'previous');
```
- **Explanation**: Samples the signal at a new sampling rate and reconstructs it using zero-order hold interpolation.
11. **Time Division Multiplexing (TDM)**
```matlab
MATLAB Syntax and Commands
multiplexed_signal = reshape(channels, 1, N*length(t));
demultiplexed_signals = reshape(multiplexed_signal, N, length(t));
```
- **Explanation**: Reshapes the matrix `channels` into a single row for multiplexing and back for demultiplexing.
12. **Quantization and Encoding**
```matlab
x_quantized = round((x_sampled + 1) * (levels - 1) / 2) / ((levels - 1) / 2) - 1;
x_encoded = de2bi(x_quantized_integer, bits, 'left-msb');
```
- **Explanation**: Quantizes the sampled signal and converts the quantized signal to binary vectors with specified
bit-length using left-most significant bit ordering.
MATLAB Syntax and Commands
Detailed Explanations of Selected Syntaxes
1. **ones(n, m)**
- Creates an `n`-by-`m` matrix of ones.
2. **square(t, duty)**
- Generates a square wave with period `T` and duty cycle `duty`.
3. **sawtooth(t, width)**
- Generates a sawtooth wave with period `T` and width `width`.
4. **fftshift(x)**
- Shifts the zero-frequency component to the center of the spectrum.
5. **hilbert(x)**
- Computes the analytic signal using the Hilbert transform.
6. **fmmod(x, fc, fs, kf)**
- Frequency modulates the message signal `x` with carrier frequency `fc`, sampling frequency `fs`, and modulation
index `kf`.
7. **fmdemod(y, fc, fs, kf)**
- Frequency demodulates the signal `y` with carrier frequency `fc`, sampling frequency `fs`, and modulation index `kf`.
MATLAB Syntax and Commands
8. **interp1(x,v,xq,method)**
- Interpolates the data `v` at the points `x` using the specified interpolation method (`previous` for zero-order hold).
9. **reshape(A, m, n)**
- Reshapes the matrix `A` into an `m`-by-`n` matrix.
10. **round(x)**
- Rounds each element of `x` to the nearest integer.
11. **de2bi(d, n, msbflag)**
- Converts decimal integers `d` to binary vectors with `n` bits and specified most significant bit order `msbflag`.
This comprehensive list covers the syntaxes and functions used in the MATLAB programs, providing both basic and
advanced signal processing techniques.