Experiment 1: Familiar with the MATLAB environment and the generation and operation of basic signals

Experiment 1: Familiar with the MATLAB environment and the generation and operation of basic signals

1. The purpose of the experiment

1. Familiar with MATLAB's operating environment and basic operating commands;
2. Master the representation of signals in MATLAB;

2. Experimental equipment

Computer, MATLAB software

3. Experimental principle

1. Introduction to MATLAB

The MATLAB language is based on matrix calculations. The grammatical rules are simple and easy to learn. It combines high-performance numerical calculation and visualization. It has a powerful, rich function toolbox and strong scalability. It is well received by engineers and technical experts. And it soon became an indispensable basic software in the fields of computer-aided analysis, design, simulation, and teaching in applied disciplines.

(1) MATLAB desktop system

MATLAB is both a high-level computer language and a programming environment. MATLAB's desktop system consists of desktop platform, window, menu bar and toolbar. After starting MATLAB, the interface is shown in Figure 1.1.
In Figure 1.1, there is the title "MATLAB" at the top, and the right side of the title bar is the window minimize, zoom and close buttons from left to right. Below the title bar is the bar main menu, and under the main menu are the toolbar buttons and the pop-up menu box for setting the current directory and the button for viewing the directory tree on the right ([Browse for Folder]). The large window under the toolbar is the main window of MATLAB. Set 4 small windows in the large window (default setting): "Workspace" is the workspace browser window, which manages the variables in the workspace. When running the MATLAB program, the program The main information of all the created variables resides in the workspace browser, providing users with a very convenient query service; "Current Directory" path browser window, which displays the files under the current path; "Command History" historical commands Window; "Command Windows" is the command window for users to work with MATLAB, and is also the main window for realizing various functions of MATLAB. MATLAB provides users with an interactive working environment here, that is, users can perform numerical calculations and symbols here. Complicated analysis and processing such as calculation and visualization of calculation results.
Insert picture description here

                                            图1.1 MATLAB桌面系统

For example, directly input the following characters in the working area of ​​the command window: a=eye(4,4) and press the Enter key to generate a 4×4 identity matrix, and the display result is shown in Figure 1.2.
Insert picture description here

                                          图1.2   程序运行结果

(2) MATLAB function commands commonly used in experiments

In the entire signal and system experiment process, MATLAB language is used to realize signal description and signal calculation. Therefore, you must have a good understanding of the simple and commonly used function commands in MATLAB. Commonly used function commands are as follows:
function : In MATLAB, it is not its own function that can complete all functions, more often it is to write a program to achieve the required function, then this command is used, the calling format is: function *** () The name of the function is outside the brackets, and the variables to be used in the function are in the brackets.
plot command : The plot command is used to draw continuous signal waveforms in MATLAB. Its function is to connect the vector points sequentially with straight lines. Calling format: plot(f), where the value of the vector f is the value of the ordinate, and the corresponding element label is the value of the abscissa; plot(t,f), t and f are vectors of the same dimension, and t is the abscissa , F is the ordinate; plot(t1,f1;t2,f2), this command can draw two signal waveforms under the same graph.
title command : In the drawing command, you can use this command to make some comments on the drawn waveform. The calling format is: title(' ……') The middle part is the description of arbitrary annotations on the graphics.
xlabel, ylabel commands : These two are also used to label the drawn waveforms, which can label the meaning of the unknowns of the two coordinate axes and increase the amount of information in the graph. Calling format: xlabel(' ……'), ylabel(' ……') The middle can be text or letters for annotation of the coordinate axis.
axis command : This command can be used to define the range of coordinates in the drawn waveform. The calling format is axis([k1,k2,g1,g2]), where k1, k2 represent the range of the abscissa, and g1, g2 represent the range of the ordinate.
stem command: This command is specially used to draw discrete sequence of waveforms. The calling format is: stem(k,f) This command can be used to draw a dot plot of a discrete sequence.
fliplr command : This function is used to reverse the vector with the zero time as the reference point. The calling format is: f=fliplr(f1) so that f is the function after the vector f1 is reversed.
min, max commands : These two commands can be used to compare and calculate the minimum and maximum values ​​in a vector, or to compare the smaller of the two values. The calling format is: min(k), max(k), min(k1,k2), max(k1,k2)
length command : This function can calculate the length of the vector. The calling format is length(f).
The ones function : This is a commonly used function in MATLAB. It generates a matrix with all 1 elements. This function is used in this article to represent a discrete step sequence, or to define a continuous gate signal. The calling format is: n = 0: 5; ones(1,n) represents a step sequence whose length is an integer n. k=1:p:8; ones(1,length(k)) represents the gate signal of length k.
Subplot command : In the MATLAB drawing process, sometimes in order to facilitate the observation of the changes in the graph, it is necessary to display the waveforms of multiple signals in a waveform display window. At this time, you can call the subplot command. The calling format is: subplot(n1,n2,k), indicating that a graph is divided into n1
n2 blocks, and the waveform is drawn on the k- th block.

2. Representation of signals in MATLAB - generally use vectors to represent signals in MATLAB

(1) Continuous time signal

Continuous time signal. Strictly speaking, MATLAB cannot process continuous signals. Instead, it approximates the continuous signal samples at equal time intervals. When the sampling time interval is small enough, discrete samples can be better approximated Continuous signal. Therefore, for the continuous time signal f(t), it is represented by two row vectors f and t, where the vector is defined as: t=t1:dt:t1, t1 is the signal start time, t2 is the end time, is the time interval. The vector f is the sample value of the continuous signal f(t) at the time point defined by the vector. For example: continuous time signal f(t)=cos(t), which can be expressed in MATLAB language as:
t=-5:0.1:5; f=cos(t);
After the continuous signal is expressed by the above vector, it can be commanded by plot Time domain waveform. The command is as follows: plot(t,f), the signal waveform is shown in Figure 1.3.
Insert picture description here

                                                图1.3 的波形

(2) Discrete time signal

Discrete time signals are generally represented by f(k), and the variable k is an integer, representing discrete sampling time points. When using MATLAB to represent the discrete sequence f(k), it is also represented by two vectors, one vector represents the value range of the independent variable k, and the other vector represents the value of the sequence f(k).
For example, the sequence f(k)={2,2,2,-1,3,0,2;k=-2,-1,0,1,2,3,4} can be expressed by MATLAB as:
k=-2 :4;f=[2,1,2,-1,3,0,2]
The waveform of this discrete time series can be drawn using the MATLAB function stem(k,f), as shown in Figure 1.4.
Insert picture description here

                                            图1.4  f(k)的波形图

3. Basic operations of signals

The basic operations of the signal refer to the time shift of the signal, the folding of the signal, the addition of the signal, and the multiplication of the signal. Many complex signals are obtained through basic operations of simple signals. The time shift, reflex and scale transformation of the signal will be introduced in later experiments.

(1) Add and subtract signals

Signal addition is the addition of two or more signals at the same point in time. For a continuous time signal, the addition of the two signals can be expressed as:
y(t)=f1(t)+f2(t)
In MATLAB, the discrete value of the continuous signal is used to add, so it is compared with the discrete time The calculation method of the signal in MATLAB is the same. Its MATLAB implementation is
y=f1+f2.
These two requirements require that the lengths of f1 and f2 are the same, and the positions correspond to each other before the addition can be performed. If the lengths of the two corresponding variables are not equal, you need to In this case, add zeros to the left and right of the signal and then add them.

(2) Signal multiplication

Signal multiplication is to multiply two or more signals at the same point in time. The mathematical expression of multiplying two continuous time signals is
y(t)=f1(t)*f2(t).
For discrete time signals, the multiplication of the signals f1(k) and f2(k) refers to the two signals at the same time The value pair of is multiplied, which can be expressed as
y(k)=f1(k)*f2(k).
The MATLAB implementation is y=f1.*f2.
Here also requires that the length of the two signals must be equal, and if they are not equal, the signal must be Add zeros to the left and right to multiply.

Four, sample demonstration

1. The basic signal waveform

Use the special functions in the Signal Processing Toolbox of MATLAB software to generate signals and draw waveforms.

(1) Sine signal

The sinusoidal signal
is represented by the sin function in MATLAB .

The program is as follows:
% Program1.1.m
t=0:0.01:3 pi;
y1=sin(2
t);
y2=cos(2*t);
plot(t,y1;t,y2)
The result is shown on the right Shown.
Insert picture description here

(2) Sampling signal Sampling signal Sa(t)=sin(t)/t

It is represented by the sinc function in MATLAB.
Defined as
% Program1.2.m
t=linspace(-5,5); %t value range
f=sinc(t);% Generate sinc function
plot(t,f)
grid on
Insert picture description here

(3) Index signal

It
is represented by exp function in MATLAB .
% Program1.3.m
t= -2:0.5:2;
y=exp(-t);
plot(t,y);% plot the continuous value
hold on;
stem(t,y);% plot the discrete value
Insert picture description here

(4) Unit step signal

% Program1.4.m
t=-10:10;
x=((t-0)>=0);
stem(t,x);
hold on;
plot(t,x,’r’);
Insert picture description here

Or compile a function in matlab. The self-compiled function used in future experiments is heaviside()
function f=heaviside(t)
f=(t>0);

(5) Unit impulse sequence

Or the expression
Insert picture description here
of the unit pulse sequence: the expression of the unit pulse sequence of delay ks:Insert picture description here

% Program1.5.m
k=-10:10
x=(k0);
t=-10:0.01:10;
x1=(t
0);
stem(k,x);% unit impulse sequence
hold on;
plot(t,x1,'r')% unit impulse functionInsert picture description here

Or compile a function by yourself in matlab, the self-compiled function is named jyxl()
function x=jyxl(n)
x=(n>=0)
or
 % unit pulse sequence m file
  clear
k0=0; kf=10; ks= 3;% In this example, take ks=3,
k1=k0:kf;
x1=[zeros(1,ks-k0),1,zeros(1,kf-ks)];% unit pulse sequence generation
stem(k1, x1,'.');title('unit pulse sequence')% drawing

(6) Triangle signal

The triangular signal is represented by the tripuls function in MATLAB.
The calling format is ft=tripuls(t,width,skew), which generates a triangular wave with an amplitude of 1, a width of width, and a center of 0 to expand width/2 to the left and right, and a slope of skew. The default value of width is 1, and the value range of skew is between -1 and +1. Generally, the maximum amplitude 1 appears at the abscissa position of t=(width/2)*skew.
% Program1.6.m
t=-3:0.01:3;
ft=tripuls(t,4,0.5);
plot(t,ft); grid on;
axis([-3,3,-0.5,1.5]) ;

(7) Complex exponential signal

*The calling format is f=exp((a+j*b) t)
% Program1.7.m
t=0:0.01:3;
a=-1;b=10;
f=exp((a+j*b )*t);
subplot(2,2,1),plot(t,real(f)),title('real part')
subplot(2,2,3),plot(t,imag(f)), title('imaginary part')
subplot(2,2,2),plot(t,abs(f)),title('module')
subplot(2,2,4),plot(t,angle(f)) ,title('phase angle')

(8) Rectangular pulse signal

The rectangular pulse signal can be generated by the rectpuls function. The
calling format is y=rectpuls(t,width), the amplitude is 1, the width is width, and t=0 is the center of symmetry.
% Program1.8.m
t=-2:0.01:2;
width=1;
ft=2*rectpuls(t,width);
plot(t,ft)
grid on;

2. Signal calculation

(1) Plot the real part signal, imaginary part signal, amplitude signal, and phase signal of the continuous sequence f(t)=exp[(-0.1+j0.5)t].

% Program1.9.m
clear all
t=-5:5;
x=-0.1+0.5 j;
f=exp(x
t);% gives complex exponential signal
real_f=real(f);% takes complex exponential signal The real part
imag_f=imag(f);% take the imaginary part of the complex exponential signal
mag_f=abs(f);% take the amplitude of the complex exponential signal
phase_f=angle(f);% take the phase of the complex exponential signal
subplot(2,2 ,1);
plot(t,real_f);
xlabel('t');title('real part');
subplot(2,2,2);
plot(t,imag_f);
xlabel('t');title ('Imaginary part');
subplot(2,2,3);
plot(t, mag_f);
xlabel('t'); title('amplitude');
subplot(2,2,4);
plot(t, phase_f);
xlabel('t'); title('phase');
The program running result is shown in Figure 1.5.
Insert picture description here

                                       图1.5 信号的不同分量

(2) Addition and multiplication of two sequences

% Program1.10.m
clear all
t=0:0.01:2;
f1=2.^(-3t);
f2=0.7
sin(4pit);
f3=f1+f2;
f4=f1.f2;
subplot(2,2,1);plot(t,f1);title(‘f1(t)’);grid on;
subplot(2,2,2);plot(t,f2);title(‘f2(t)’);grid on;
subplot(2,2,3);plot(t,f3);title(‘f1+f2’); grid on;
subplot(2,2,4);plot(t,f4);title('f1
f2’); grid on;
其运行结果如图1.6所示
Insert picture description here

                                                     图1.6  信号的运算

Five, experiment content

1. Verify the procedure in the experiment principle

2. Draw the signal waveform (1) (2)

3. Choose two simple signals for addition, subtraction and multiplication.

(1). Use matlab to realize f=f1(t)+f2(t) and f=f1(t).f2(t)

The procedure is as follows...
clear all;
t=0:0.0001:5;
f1=(-t+4);
f2=stepfun(t,0)-stepfun(t,4);
f3=f1.*f2;
f4=f1+ f2;
subplot(2,1,1),plot(t,f3),title('f1.f2')
subplot(2,1,1),plot(t,f3),title('f1+f2')
The results are as follows
Insert picture description here

(2). Realize with matlabInsert picture description here

The procedure is as follows...
t = -10:10;
t0 = 0;
m = 2-exp(-2*t);
n = stepfun(t,t0);
y = m.*n;
stem(t,y);
hold on
plot(t,y,'r'); The
running results are as follows
Insert picture description here

(3). Realize with matlabInsert picture description here

The procedure is as follows...
t = -10:10;
t0 = 0;
f1 = (1+cos(pi*t));
f2 = stepfun(t,t0)-stepfun(t,t0+2);
y = f1.* f2;
stem(t,y);
hold on
plot(t,y); The
running results are as follows
Insert picture description here

Six, experiment report requirements

1. Briefly describe the purpose and principle of the experiment.

2. Write a program (M file) for the experiment content and draw the waveform of the signal after the operation.

3. Write the experience in the experiment.

This is the first experiment in the signal class. The first learning is relatively difficult. There is no MATLAB software, no experimental experience, and no knowledge of how to program design. Faced with all kinds of problems, I calmed my mind, listened to the teacher's lecture seriously, and delved into it after class. I learned how to use MATLAB to generate basic signals, draw signal waveforms, and realize basic operations of signals, which laid the foundation for the subsequent signal analysis and system design, and gained a lot. I believe that my unremitting efforts will definitely improve my experimental hands-on ability and understanding of signals.

Guess you like

Origin blog.csdn.net/qq_45696377/article/details/109909420