Matlab signal processing notes

During the winter vacation, I started to learn signal processing based on MATLAB. Learning materials: insert image description here
record with this blog

1. MATLAB basics

1. Matlab selection structure - switch-case statement

Matlab basic function-fix function

%例1.8 编写一个某地产公司对顾客所购买的房产实行打折销售的标准
price=input('请输入商品价格:')
switch fix(price/100)
    case {
    
    0,1}
        rate=0;
    case {
    
    2,3,4}
        rate=0.1/100;
    case num2cell(5:9)
        rate=0.2/100;
    case num2cell(10:24)
        rate=0.3/100;
end
price=price*(1-rate)

Different from the switch-case statement of the C language, in the MATLAB language, when the condition after one of the case statements is true, the switch-case statement does not judge the subsequent case statement, that is to say, in the MATLAB language, Even if multiple case judgment statements are true, only the first true statement encountered is executed. In this way, it is not necessary to add a break statement after each case statement to prevent the subsequent execution of the true case conditional statement like in the C language.

2. The difference between the break statement and the continue statement

break statement: abort this loop and jump out of the innermost loop (that is, no longer execute the loop)
continue statement: skip this loop and execute the next loop

%break语句
a=0;b=1;
for i=0:5
    b=b+1
    if i>2
        break
    end
    a=a+2
end

%运行结果:
a=0,2,4,6
b=1,2,3,4,5
%continue语句
a=0;b=1;
for i=0:5
    b=b+1
    if i>2
        continue
    end
    a=a+2
end

%运行结果:
a=0,2,4,6
b=1,2,3,4,5,6,7

3. try-catch statement

MATLAB try-catch structure

%try-catch语句
%例1.12 先求两矩阵的乘积,若出错,则自动转去求两矩阵的点乘
A=[1,2;3,4];
B=[5,6;7,8];

try
    C=A*B;
catch
    C=A.*B;
end
C

4. Arrays and matrices

4.1 Arrays

Use "[]" to create a one-dimensional array. The row array elements are separated by spaces or commas, and the column array elements are separated by semicolons . The punctuation marks must be entered in English.

%创建数组实例
clear all
A=[]
B=[6 4 3 5 6 7] %行数组元素用空格或者逗号分隔
C=[4,2,4,5,6,7]
D=[1;4;56;67;75;3] %列数组元素用分号分隔
E=B' %转置
E=B.' %共轭转置

%程序结果
A =

     []


B =

     6     4     3     5     6     7


C =

     4     2     4     5     6     7


D =

     1
     4
    56
    67
    75
     3


E =

     6
     4
     3
     5
     6
     7


E =

     6
     4
     3
     5
     6
     7
%1.18 访问数组实例
clear all
clc
B=[1 2 3 5 7 6]
b1=B(1) %访问数组第1个元素
b2=B(1:3) %访问数组第1个到第3个元素
b3=B(3:end) %访问数组第3个到最后1个元素
b4=B(end:-1:1) % 数组元素反向输出
b5=B([1 4])  %访问数组第1个和第4个元素

%程序结果
B =

     1     2     3     5     7     6


b1 =

     1


b2 =

     1     2     3


b3 =

     3     5     7     6


b4 =

     6     7     5     3     2     1


b5 =

     1     5

%1-19 对一子数组赋值
clear all
clc
B=[1 2 3 5 7 6]
B(3)=34 %访问数组第3个元素  将第3个元素赋值为34
B([4 6])=[56 987] %访问数组第4个和第6个元素 分别赋值为56,987

%程序结果
B =

     1     2     3     5     7     6


B =

     1     2    34     5     7     6


B =

     1     2    34    56     7   987

4.1.1 Create a one-dimensional array with a colon

The specific format is: x1:x2:x3, where x1 represents the initial value, x2 represents the step size, and x3 represents the end value. When using a colon to generate an array, if x2 is a negative value, it means that a decreasing vector is generated; if x2 is omitted, the default step size of the system is 1.

%1-20 用冒号创建一维数组
clear all
clc

A=3:6 %创建元素为3到6数组
B=3.1:1.5:6 %创建元素为3.1到6,步长为1.5的数组
C=3.1: -1.5 :-6%创建元素为3.1到-6,步长为-1.5的数组

%运行结果
A =

     3     4     5     6


B =

    3.1000    4.6000


C =

    3.1000    1.6000    0.1000   -1.4000   -2.9000   -4.4000   -5.9000

4.1.2 Create a one-dimensional array with the logspace() function

The logspace() function is commonly used in MATLAB to create a one-dimensional array, and the calling method is as follows:
(1) y=logspace(a, b); This function creates a row vector y, the first element is 10 a, the last element is 10 b, Forms a geometric sequence that is always 50 elements.
(2) y=logspace(a, b, n); This function creates a row vector y, the first element is 10 a, and the last element is 10 b, forming a geometric sequence that is always n elements.

%1-21 用logspace()创建一维数组
clear all
clc
A=logspace(1,4,20) %创建一个由区间 [10^1,10^4] 内的20个点组成的向量

%运行结果
A =

   1.0e+04 *

  列 111

    0.0010    0.0014    0.0021    0.0030    0.0043    0.0062    0.0089    0.0127    0.0183    0.0264    0.03791220

    0.0546    0.0785    0.1129    0.1624    0.2336    0.3360    0.4833    0.6952    1.0000

Supplementary basis:
1.0e+04 in the running result is 1 10^4=10000
, which is the representation of scientific notation.
Two more examples:
If you enter a vector [23 000 000, 55 000 000], then MATLAB will express it as: 1.0e+7
[2.3, 5.5]
If you enter a vector [0.000 000 23, 0.000 000 55] , then MATLAB will express it as: 1.0e-7*[2.3, 5.5]

4.1.3 Create a one-dimensional array with the linspace() function

y = linspace(x1,x2) returns a row vector containing 100 equally spaced points between x1 and x2.
y = linspace(x1,x2,n) generates n points. The points are spaced at (x2-x1)/(n-1).
linspace is like the colon operator ":", but allows direct control over the number of points and always includes endpoints. The "lin" in the name of "linspace" indicates that linearly spaced values ​​are produced instead of the sibling function logspace, which produces logarithmically spaced values.

%1-22 用linspace()创建一维数组
clear all
clc
A=linspace(1,20,5)%创建一个由区间 [1,20] 中的5个等间距点组成的向量
B=linspace(1,2)%创建一个由区间 [1,2]中的100个等间距点组成的向量

%运行结果
A =

    1.0000    5.7500   10.5000   15.2500   20.0000


B =111

    1.0000    1.0101    1.0202    1.0303    1.0404    1.0505    1.0606    1.0707    1.0808    1.0909    1.10101222

    1.1111    1.1212    1.1313    1.1414    1.1515    1.1616    1.1717    1.1818    1.1919    1.2020    1.21212333

    1.2222    1.2323    1.2424    1.2525    1.2626    1.2727    1.2828    1.2929    1.3030    1.3131    1.32323444

    1.3333    1.3434    1.3535    1.3636    1.3737    1.3838    1.3939    1.4040    1.4141    1.4242    1.43434555

    1.4444    1.4545    1.4646    1.4747    1.4848    1.4949    1.5051    1.5152    1.5253    1.5354    1.54555666

    1.5556    1.5657    1.5758    1.5859    1.5960    1.6061    1.6162    1.6263    1.6364    1.6465    1.65666777

    1.6667    1.6768    1.6869    1.6970    1.7071    1.7172    1.7273    1.7374    1.7475    1.7576    1.76777888

    1.7778    1.7879    1.7980    1.8081    1.8182    1.8283    1.8384    1.8485    1.8586    1.8687    1.87888999

    1.8889    1.8990    1.9091    1.9192    1.9293    1.9394    1.9495    1.9596    1.9697    1.9798    1.9899100

    2.0000

4.2 Common array operations

4.2.1 Addition, subtraction, multiplication and division of arrays

insert image description here

%例23-27 数组的运算
clear all
clc
A=[2 3 5 6 7 8]
B=[1 4 6 3 7 2]

C=A+B %数组的加法
D=A-B %数组的减法
E=A.*B %数组的乘法
F=A./B %数组的左除A/B
G=A.\B %数组的右除B/A
F=A.^B %数组的求幂

%运行结果
A =

     2     3     5     6     7     8


B =

     1     4     6     3     7     2


C =

     3     7    11     9    14    10


D =

     1    -1    -1     3     0     6


E =

     2    12    30    18    49    16


F =

    2.0000    0.7500    0.8333    2.0000    1.0000    4.0000


G =

    0.5000    1.3333    1.2000    0.5000    1.0000    0.2500


F =

           2          81       15625         216      823543          64

4.2.2 Dot Product of Arrays

C = dot(A,B) returns the scalar dot product of A and B.
If A and B are vectors, they must have the same length.
C = dot(A,B,dim) computes the dot product of A and B along dimension dim. The dim input is a positive integer scalar.

%1.27 数组的点积
clear all
clc
A=[2 4 5]
B=[1 4 2]
a=dot(A,B) %数组的点积
b=sum(A.*B) %数组元素的乘积之和

%运行结果
A =

     2     4     5
B =

     1     4     2
a =

    28
b =

    28

4.2.3 Relational operations on arrays

MATLAB array operations
There are 6 descriptions of the relationship between two numbers in MATLAB

less than <
more than the >
equal to ==
greater or equal to >=
less than or equal to <=
not equal to ~=

When comparing the size of two elements, if the expression is true, return the result 1, otherwise return 0

%数组的关系运算
clear all
clc
A=[1 3 4 5 6]
B=[1 2 4 54 5]
C=A>B
D=A<B
E=A==B
F=A>=B
G=A<=B

%运行结果
A =

     1     3     4     5     6
B =

     1     2     4    54     5
C =

  1×5 logical 数组

   0   1   0   0   1
D =

  1×5 logical 数组

   0   0   0   1   0
E =

  1×5 logical 数组

   1   0   1   0   0
F =

  1×5 logical 数组

   1   1   1   0   1
G =

  1×5 logical 数组

   1   0   1   1   0

4.3 Matrix

4.3.1 Matrix Representation

(1) Direct input method

%直接输入法创建矩阵
clear all
clc
A = [1 34 45;12 23 54;65 32 6]%行与行之间既可以通过;号隔开,也可以通过回车符隔开
B=[1 2 4
   32 43 54
   54,65,756]

%运行结果
A =

     1    34    45
    12    23    54
    65    32     6

B =

     1     2     4
    32    43    54
    54    65   756

(2) Load external data files

The file content of data.txt:
insert image description here

%读取数据文件data.txt
clear all
clc
load data.txt %载入数据文件
imagesc(data)%以图像的形式显示
colormap winter %查看并设置当前颜色图为"winter"冷色调

Running results:
insert image description here
Supplement: MATLAB | MATLAB color matching is not enough. The most complete colormap supplementary package on the whole network is here. When writing papers, you can refer to modifying color matching

(3) Create a matrix using MATLAB built-in functions

Function Name Function Introduction
ones() Generates a matrix of all 1s
zeros() Generates a matrix of all 0s
eye() Generates an identity matrix
rand() Generates a random matrix distributed in the (0,1) interval
randn() Generates a mean of 0 , standard normal distribution random matrix with variance 1
compan() companion matrix
gallery() Higham test matrix
hadamard() Hadamard matrix
hankel() Handkel matrix
hilb() Hilbert matrix
magic() magic square matrix
pascal() pascal matrix
rosser( ) classical symmetric eigenvalue
toeplitz() Toeplitz frame
vander() vander matrix
wilknsion() wilknsion eigenvalue test matrix

%利用系统内置函数创建矩阵
clear all
clc
A=zeros(5,4) %5行4列全为0的矩阵
close all
B=randn(5,4) %5行4列均值为0,方差为1的标准正态分布随机矩阵
imagesc(B)%以图像的形式显示
colormap winter %查看并设置当前颜色图为"winter"冷色调

%运行结果
A =

     0     0     0     0
     0     0     0     0
     0     0     0     0
     0     0     0     0
     0     0     0     0
B =

   -1.0891    0.0859   -0.6156   -1.4023
    0.0326   -1.4916    0.7481   -1.4224
    0.5525   -0.7423   -0.1924    0.4882
    1.1006   -1.0616    0.8886   -0.1774
    1.5442    2.3505   -0.7648   -0.1961

operation result:
insert image description here

(4) Create and save matrix using M file

Create an M file, enter the matrix in it, save it as AA.m, and then enter AA on the command line.

%利用M文件创建和保存矩阵 创建A1_32.m文件
A=[12 324 435
   213 43 45
   2 4 54]

operation result:
insert image description here

4.3.2 Matrix search

(1) Single element search
%矩阵的单元素寻访
clear all
clc
A=[12 324 435
   213 43 45
   2 4 54]
x=A(1,1)
y=A(2,2)
z=A(3,1)

%运行结果
A =

    12   324   435
   213    43    45
     2     4    54
x =
    12
y =
    43
z =
     2
(2) Multi-element search

(1) A(e1:e2:e3) means to take all the elements from the e1th element of the array or matrix A to e3 every e2 steps; (2) A([mnl]) means to take the array or
matrix A
(3) A(:,j) means to take all the elements of the j-th column of the A matrix; ( 4
) A(i,:) means to take all the elements of the i-th column of the A matrix;
(5) A(i:i+m,:) means to take all the elements in row i~(i+m) of matrix A; (6)
A(:,k,k+m) means to get all elements of row k~ All elements in (k+m) column;
(7) A(i:i+m,k:k+m) means to take the i~(i+m) row of the A matrix, and the k~(k+m) ) all the elements in the column;
(8) It is also possible to use the general vector and end operator to express the matrix subscript, so as to obtain the sub-matrix. end indicates the subscript of the end element of a certain dimension.

%对矩阵进行多元素寻访
clear all
clc
A=randn(4) %生成均值为0,方差为1的标准正态分布随机矩阵
A(1,:) %访问第1行所有元素
A(2:4,:) %访问第2到4行所有元素
A(:,2) %访问第2列所有元素
A %访问所有元素

%运行结果
A =

   -1.5771   -1.3337    0.0229   -0.8314
    0.5080    1.1275   -0.2620   -0.9792
    0.2820    0.3502   -1.7502   -1.1564
    0.0335   -0.2991   -0.2857   -0.5336

ans =

   -1.5771   -1.3337    0.0229   -0.8314
ans =

    0.5080    1.1275   -0.2620   -0.9792
    0.2820    0.3502   -1.7502   -1.1564
    0.0335   -0.2991   -0.2857   -0.5336
ans =

   -1.3337
    1.1275
    0.3502
   -0.2991
A =

   -1.5771   -1.3337    0.0229   -0.8314
    0.5080    1.1275   -0.2620   -0.9792
    0.2820    0.3502   -1.7502   -1.1564
    0.0335   -0.2991   -0.2857   -0.5336

4.3.3 Operations on matrices

The syntax is similar to array operations, see 4.2.

5. Graphic drawing

5.1 Drawing of two-dimensional graphics

In MATLAB, the main two-dimensional plotting functions are as follows:

(1) Drawing of ordinary two-dimensional graphics

%1.50在同一坐标轴上绘制sin(x)、sin(2x)、sin(3x)这三条曲线
clear all
clc
x=0:0.01:3*pi
y1=sin(x)
y2=sin(2*x)
y3=sin(3*x)

figure
plot(x,y1,x,y2,x,y3)
axis([0 8 -1.5 1.5]) %设置坐标轴范围
grid on

xlabel('x')
ylabel('y')
title('演示绘图基本步骤')
legend('sin(x)','sin(2x)','sin(3x)')

operation result:
insert image description here

%显示函数y=2*exp(-0.5*x).*sin(2*pi*x)及其包络线
clear all
clc
x=(0:pi/100:2*pi)'
y1=2*exp(-0.5*x)*[1,-1]%上下两条包络线
y2=2*exp(-0.5*x).*sin(2*pi*x)%函数
x1=(0:12)/2
y3=2*exp(-0.5*x1).*sin(2*pi*x1)
figure
plot(x,y1,'-.',x,y2,'y-',x1,y3,'rp')

Operation result:
insert image description here
Complementary basis:
(1) ' Table transposition
(2). : Dot multiplication is the operation of multiplying each element of the vector
Syntax:

.Operation: [1 2 3].*[4 5 6]
Result: [4 10 8]
(3) The dot product is to multiply the corresponding elements of the vector and add them up
Syntax: dot(x1,x2)
Vector dot product, dot product, and cross product calculation in Matlab

(2) Drawing of double ordinate graphics

yyaxis left activates the side of the current axes associated with the left y-axis. Subsequent graphics commands target the left side.
yyaxis right activates the side of the current axes associated with the right y-axis. Subsequent graphics commands target the right side.

%绘制双纵坐标图形
x=linspace(0,4)
y=x
yyaxis left
plot(x,y,'b-.')

x1=0:pi/10000:7*pi
y1=sin(x1*2*pi)
yyaxis right
plot(x1,y1,'r.')

operation result:
insert image description here

Supplementary basis:
  1. text: add a text description to the data point
% 绘制一条正弦曲线。在点 (π,0) 处,添加文本说明 sin(π)
x = 0:pi/20:2*pi;
y = sin(x);
plot(x,y)
text(pi,0,'\leftarrow sin(\pi)') %使用 \pi 表示希腊字母 π。使用 \leftarrow 显示一个向左箭头。

operation result:
insert image description here

  1. gtext: add text to a figure using the mouse
% 绘制一条正弦曲线。在点 (π,0) 处,添加文本说明 sin(π)
x = 0:pi/20:2*pi;
y = sin(x);
plot(x,y)
gtext('\leftarrow sin(\pi)','color','red','fontsize',14) %使用 \pi 表示希腊字母 π。使用 \leftarrow 显示一个向左箭头。color设置颜色属性为红色。fontsize设置字体大小为14

operation result:
insert image description here

2. Fundamentals of signal and system analysis

1. The concept of discrete time signal

Discrete-time signal: It is a discrete value at time t.
Discrete in time, continuous in amplitude. (At any two discrete time points, the difference between the two amplitudes is infinitely small). Such as sampling signals.
Discrete in time and discrete in amplitude. Defined as a digital signal.
Discrete-time signals can often be represented by the following figure:
insert image description here
Although the abscissa in the figure is a continuous line, it is important to know that x[n] is only defined when n is an integer value. It is considered that x[n] is in n Incorrect if not integer is zero; x[n] is simply undefined when n is non-integer.

%用图示法来表示离散时间信号
clc
clear all
N=[-3 -2 -1 0 1 3 3 2 5 6 7 6 9 11]
X=[0 2 3 3 2 3 0 -1 -2 -3 -4 -5 1 2]
subplot(211)
stem(N,X) %绘制离散针状图
hold on
plot(N,zeros(1,length(X)),'r') %绘制横轴
set(gca,'box','on')%将图轴方框绘制出来
xlabel('序列号');ylabel('序列值')
subplot(212)
plot(N,X,'r')
hold on
plot(N,zeros(1,length(X)),'b')
xlabel('时间/s');ylabel('函数值')

operation result:
insert image description here

2. Sampling Theorem

insert image description here

% 时域的信号长度,决定频域的采样间隔,它们成导数关系
% 时域中信号有N点,每点间隔dt,所以时域信号长度为N*dt
% 那么频谱每点的间隔就是1/(N*dt)

clc
clear all

dt=0.01;n=0:90-1
t=dt*n
f=10 %原始信号频率为10HZ
x=sin(3*pi*f*t+0.5)%原始信号

dt=0.1;n=0:10-1
t1=dt*n
x1=sin(3*pi*f*t1+0.5)%采样后的信号
subplot(311);plot(t,x);title('原始信号')
subplot(312);plot(t,x,t1,x1,'rp');title('采样过程')
subplot(313);plot(t1,x1);title('抽样后的信号')

operation result:
insert image description here

2.3 Discrete time series

Guess you like

Origin blog.csdn.net/weixin_44769034/article/details/128482895