Zen的小站

小舟从此逝,江海寄余生

0%

【matlab】学习

matlab语句,随用随记

matlab官网

数据类型

cell

  1. 创建

    1
    2
    3
    % 有两种方式
    a={'winter',123,'coming','哈哈'};
    a=cell(1,4); a={'winter',123,'coming','哈哈'};
  2. 访问

    1
    a{1};
  3. 相关函数

    1. cell2mat:转化为数组,要求 cell 内变量数据类型相同
    2. cellplot:用彩图显示内容
    3. cellfunA = cellfun(func,C) 将函数 func 应用于元胞数组 C 的每个元胞的内容

函数

tf函数:创建传递函数

tf创建实值或复值传递函数模型,或转换动态系统模型传递函数形式。

语法用法
sys = tf(numerator,denominator)创建连续时间传递函数模型
sys = tf(numerator,denominator,ts)创建离散时间传递函数模型

例如$sys(s)=\frac1{2s^2+3s+4}$可以由下面的sys表示

1
2
3
numerator = 1;
denominator = [2,3,4];
sys = tf(numerator,denominator)

绘制传递函数的波特图用margin(sys);一句话搞定

若想用该传递函数处理信号,可以用这个方法 z变换 --> 提取分子分母系数 --> 数据类型转换 --> 进行滤波

1
2
3
4
5
6
7
rc = tf(1, [R*C,1]);        % 生成传递函数
% margin(rc); % 显示传递函数波特图
drc = c2d(rc, 1/Fs); % 进行z变换
[a ,b] = tfdata(drc); % 得到分子分母系数
a=cell2mat(a(1,1)); % 数据类型转换
b=cell2mat(b(1,1));
x = filter(a, b, x); % 进行滤波

拟合

  1. p = polyfit(x, y, 2):输入两组数 x,y 拟合;2 表示用二次函数拟合;返回 p 是对应系数,如 p(1) 对应 x^2 的系数,p(2) 对应 x 的系数
  2. yi = polyval(p,xi):输入拟合次方系数和输入数组,得到输出值

方程求解:solve 函数

1
2
3
4
syms i
pi = 0.5*i^2 + 3*i + 1;

s = solve(pi == 0.8602,i);

得到方程的解

绘图

stem绘制离散信号

stem(y)orstem(x,y)

square绘制方波信号

1
2
T=0:0.01:8*pi;
y=square(T);

调整画面

  • title(‘文字’,’position’,[5,1], ‘Fontsize’,16 )

3D动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
w = pi;
z=0:0.01:3;
pic_num=1;
for t=0:0.05:5
Ex=sin(2*pi*t-k*z)+2; %螺旋线
Ey=cos(2*pi*t-k*z)+2;
Lx=Ex(1):0.01:4; %直线辅助线
Ly=0:0.01:Ey(1);

plot3(z,Ex,Ey, z,Ex,Ey*0, z,Ex*0+4,Ey, Lx*0,Lx,Lx*0+Ey(1), Ly*0,Ly*0+Ex(1),Ly); %画3维图
hold on

quiver3(0,2,2,z(1),Ex(1)-2,Ey(1)-2); %矢量箭头
axis([-1 4 0 4 0 4]);

hold off
F=getframe; %抓取当前图像
I=frame2im(F);
[I,map]=rgb2ind(I,256);%因gif不支持三维数据,所以应调用 rgb2ind,将rgb图像转为关联256种色彩的索引图像
if t==0
imwrite(I,map,'复指数信号.gif','gif'); %创建一个.gif文件,将索引图像数组和其关联的颜色图写入
else
imwrite(I,map,'复指数信号.gif','gif','WriteMode','append','DelayTime',0.01);%将多个图像并入第一个,帧间加入0.01s的延时
end
pic_num=pic_num+1;
end
  • 绘制3D曲线:plot3函数
    • 可以依次写多组xyz,绘制多条曲线
  • 曲线的取0的维度,用正常维度的数组*0得到

  • 保存gif语句

完全参考: (105条消息) MATLAB:绘制三维偏振光动画_qq_43208092的博客-CSDN博客

图像处理

  1. 读取图像 smallImage = imread('small_image.jpg');
  2. 显示图像 imshow(mergedImage);

    音频处理

  3. 读取音频audioreadgetaudiodata

    [y,Fs] = audioread(filename)

    y = getaudiodata(recObj)

  4. 播放音频soundgetaudiodata

    sound(y,Fs)

    play(recObj)

  5. 生成音频文件audiowrite

    audiowrite(filename,y,Fs)

  6. 录制音频audiorecorderrecordblocking

    1
    2
    3
    4
    recObj = audiorecorder;		% audiorecorder(Fs,16,1);
    disp('Start speaking.')
    recordblocking(recObj, 3); %录制 3s 的数据
    disp('End of Recording.');

多想多做,发篇一作

-------------本文结束感谢您的阅读-------------
// 在最后添加