当前位置:文档之家› MATLAB串口相关函数

MATLAB串口相关函数

MATLAB串口相关函数
MATLAB串口相关函数

概要

serial 创建一个串口对象,格式:s = serial('coml' )

fopen 打开串口对象,格式:fope n( s)

fread 读取串口数据,格式: fread(s)

fclose 关闭串口对象,格式:fclose(s)

free 解除Matlab对串口对象的控制,使

serial 其他程序能对该串口进行读写操作

delete 删除对象s,格式:delete( s)

clear 从工作空间中删除对象s,格式:clear(s)

fopen

Open file, or obtain information about open files

Syntax

fileID = fopen(filename)

fileID = fopen(filename, permission)

fileID = fopen(filename, permission, machineformat)

fileID = fopen(filename, permission, machineformat, encoding) [fileID, message] = fopen(filename, ...)

fIDs = fopen('all')

[filename, permission, machineformat, encoding] = fopen(fileID) Description

fileID = fopen(filename) opens the file filename for read access, and returns an integer file identifier.

fileID = fopen(filename, permission) opens the file with the specified permission.

fileID= fopen(filename, permission, machineformat) specifies the order for reading or writing bytes or bits in the file.

fileID= fopen(filename, permission, machineformat, encoding) specifies the character encoding scheme associated with the file.

[fileID, message] = fopen(filename, ...) opens a file. If the operation fails, message is a system-dependent error message. Otherwise, message is an empty string.

fIDs= fopen('all') returns a row vector containing the file identifiers of all open files.

[filename, permission, machineformat, encoding] = fopen(fileID) returns the file name, permission, machine format, and encoding that a previous call to fopen used when it opened the specified file. fopen does not read information from the file to determine these output values. An invalid fileID returns empty strings for all output arguments.

Input Arguments

filename String in single quotation marks that specifies the

name of the file to open. Can include a full or partial

path.

On UNIX systems, if filename begins with '~/' or

'~username/', the fopen function expands the path to

the current or specified user's home directory,

respectively.

If you open a file with read access and fopen cannot

find filename in the current folder, fopen searches

along the MATLAB search path. Otherwise, fopen

creates a file in the current directory.

permission String that describes the type of access for the file:

read, write, append, or update. Also specifies

whether to open files in binary or text mode.

To open files in binary mode, specify one of the

following:

'r' Open file for reading (default).

'w' Open or create new file for writing. Discard

existing contents, if any.

'a' Open or create new file for writing. Append data

to the end of the file.

'r+' Open file for reading and writing.

'w+' Open or create new file for reading and writing.

Discard existing contents, if any.

'a+' Open or create new file for reading and writing.

Append data to the end of the file.

'A' Append without automatic flushing. (Used with

tape drives.)

'W' Write without automatic flushing. (Used with

tape drives.)

To read and write to the same file:

?Open the file in update mode (with a permission

that includes a plus sign, '+').

?Call fseek or frewind between read and write

operations. For example, do not call fread

followed by fwrite, or fwrite followed by

fread, unless you call fseek or frewind between

them.

To open files in text mode, attach the letter 't' to

the permission, such as 'rt' or 'wt+'. For better

performance, do not use text mode. The following

applies on Windows systems, in text mode:

?Read operations that encounter a carriage

return followed by a newline character ('\r\n')

remove the carriage return from the input.

?Write operations insert a carriage return

before any newline character in the output.

This additional processing is unnecessary for most

cases. All MATLAB import functions, and most text

editors (including Microsoft Word and WordPad),

recognize both '\r\n' and '\n' as newline sequences.

However, when you create files for use in Microsoft

Notepad, end each line with '\r\n'. For an example,

see fprintf.

machineformat String that specifies the order for reading or writing

bytes or bits in the file. Specify machineformat to:

?Read a file created on a different system.

?Read bits in a particular order.

?Create a file for use on a different system.

Possible values are:

'n' or 'native' The byte ordering that your system

uses (default)

'b' or 'ieee-be' Big-endian ordering

'l' or 'ieee-le' Little-endian ordering

's' or

'ieee-be.l64' Big-endian ordering, 64-bit data type

'a' or

'ieee-le.l64' Little-endian ordering, 64-bit data type

Windows systems use little-endian ordering, and most

UNIX systems use big-endian ordering, for both bytes

and bits.

encoding String that specifies the character encoding scheme

to use for subsequent read and write operations,

including fscanf, fprintf, fgetl, fgets, fread, and

fwrite.

Supported values are:

'Big5' 'ISO-8859-1' 'windows-932'

'EUC-JP' 'ISO-8859-2' 'windows-936'

'GBK' 'ISO-8859-3' 'windows-949'

'Macintosh' 'ISO-8859-4' 'windows-950'

'Shift_JIS' 'ISO-8859-9' 'windows-1250'

'US-ASCII' 'ISO-8859-13' 'windows-1251'

'UTF-8' 'ISO-8859-15' 'windows-1252'

'windows-1253'

'windows-1254'

'windows-1257'

For a list of additional encoding character sets, see

https://www.doczj.com/doc/f97806207.html,/assignments/character-sets. If

you specify a value for encoding that is not in the

list of supported values, MATLAB issues a warning.

Specifying other encodings sometimes (but not always)

produces correct results.

Default: system-dependent

Output Arguments

fileID An integer that identifies the file for all subsequent

low-level file I/O operations. If fopen cannot open

the file, fileID is -1.

MATLAB reserves file identifiers 0, 1, and 2 for

standard input, standard output (the screen), and

standard error, respectively. When fopen

successfully opens a file, it returns a file

identifier greater than or equal to 3.

message A system-dependent error message when fopen cannot

open the specified file. Otherwise, an empty string.

fIDs Row vector containing the identifiers for all open

files, except the identifiers reserved for standard

input, output, and error. The number of elements in

the vector is equal to the number of open files. filename Name of the file associated with the specified fileID. permission The permission that fopen assigned to the file

specified by fileID.

machineformat The value of machineformat that fopen used when it

opened the file specified by fileID.

encoding The character encoding scheme that fopen associated

with the file specified by fileID.

The value that fopen returns for encoding is a

standard character encoding scheme name. It is not

always the same as the encoding argument that you used

in the call to fopen to open the file. Examples

Open a file. Pass the file identifier, fid, to other file I/O functions to read data and close the file.

fid = fopen('fgetl.m');

tline = fgetl(fid);

while ischar(tline)

disp(tline);

tline = fgetl(fid);

end

fclose(fid);

Create a prompt to request the name of a file to open. If fopen cannot open the file, display the relevant error message.

fid = -1;

msg = '';

while fid < 0

disp(msg);

filename = input('Open file: ', 's');

[fid,msg] = fopen(filename);

end

Open a file to write Unicode? characters to a file using the Shift-JIS character encoding scheme:

fid = fopen('japanese_out.txt', 'w', 'n', 'Shift_JIS');

serial

Create serial port object

Syntax

obj = serial('port')

obj = serial('port','PropertyName',PropertyValue,...) Description

obj = serial('port') creates a serial port object associated with the serial port specified by port. If port does not exist, or if it is in use, you will not be able to connect the serial port object to the device.

Port object name will depend upon the platform that the serial port is on. instrhwinfo ('serial') provides a list of available serial ports. This list is an example of serial constructors on different platforms:

obj = serial('port','PropertyName',PropertyValue,...) creates a serial port object with the specified property names and property values. If an invalid property name or property value is specified, an error is returned and the serial port object is not created.

Tips

When you create a serial port object, these property values are automatically configured:

?The Type property is given by serial.

?The Name property is given by concatenating Serial with the port specified in the serial function.

?The Port property is given by the port specified in the serial function.

You can specify the property names and property values using any format supported by the set function. For example, you can use property name/property value cell array pairs. Additionally, you can specify property names without regard to case, and you can make use of property name completion. For example, the following commands are all valid on a Windows platform.

s = serial('COM1','BaudRate',4800);

s = serial('COM1','baudrate',4800);

s = serial('COM1','BAUD',4800);

Refer to Configuring Property Values for a list of serial port object properties that you can use with serial.

Before you can communicate with the device, it must be connected to obj with the fopen function. A connected serial port object has a Status property value of open. An error is returned if you attempt a read or write operation while the object is not connected to the device. You can connect only one serial port object to a given serial port.

Examples

This example creates the serial port object s1 associated with the serial port COM1 on a Windows platform.

s1 = serial('COM1');

The Type, Name, and Port properties are automatically configured.

get(s1,{'Type','Name','Port'})

ans =

'serial' 'Serial-COM1' 'COM1'

To specify properties during object creation

s2 = serial('COM2','BaudRate',1200,'DataBits',7);

fread

Read data from binary file

Syntax

A = fread(fileID)

A = fread(fileID, sizeA)

A = fread(fileID, sizeA, precision)

A = fread(fileID, sizeA, precision, skip)

A = fread(fileID, sizeA, precision, skip, machineformat)

[A, count] = fread(...)

Description

A = fread(fileID) reads data from a binary file into column vector A and positions the file pointer at the end-of-file marker.

A = fread(fileID, sizeA) reads sizeA elements into A and positions the file pointer after the last element read. sizeA can be an integer, or can have the form [m,n].

A = fread(fileID, sizeA, precision) interprets values in the file according to the form and size described by the precision. The sizeA parameter is optional.

A= fread(fileID, sizeA, precision, skip) skips skip bytes after reading each value. If precision is bit n or ubit n, specify skip in bits. The sizeA parameter is optional.

A= fread(fileID, sizeA, precision, skip, machineformat) reads data with the specified machineformat. The sizeA and skip parameters are optional.

[A, count] = fread(...) returns the number of elements fread reads into A.

Input Arguments

fileID An integer file identifier obtained from fopen.

sizeA Dimensions of the output array A. Specify in one of

the following forms:

inf Column vector with the number of elements in

the file. (default)

n Column vector with n elements. If the file has

fewer than n elements, fread pads A with zeros.

[m,n] m-by-n matrix, filled in column order. If the

file has fewer than m*n elements, fread pads

A with zeros. n can be inf, but m cannot.

precision String that specifies the form and size of the values

to read. Optionally includes the class for the output

matrix A.

Use one of the following forms:

'source' Specifies class of input values.

Output matrix A is class double.

Example: 'int16'

'source=>output' Specifies classes of input and

output.

Example: 'int8=>char'

'*source' Output has the same class as

input.

Example: '*uint8'

For 'bit n' or 'ubit n'

precisions, output has the

smallest class that can contain

the input.

Example: '*ubit18' is

equivalent to 'ubit18=>uint32'

'N*source' OR

'N*source=>output' Valid if you specify a skip parameter. Read N values before each skip.

Example: '4*int8'

The following table shows possible values for source and output.

long and ulong are 32 bits on 32-bit systems, and 64

bits on 64-bit systems.

For most source precisions, if fread reaches the end

of the file before reading a complete element, fread

does not return a value for the final element.

However, if source is bit n or ubit n, then fread

returns a partial result for the final element.

Default: 'uint8=>double'

skip Number of bytes to skip after reading each value. If

you specify a precision of bit n or ubit n, specify skip

in bits. Use this parameter to read data from

noncontiguous fields in fixed-length records.

Default: 0

machineformat String that specifies the order for reading bytes

within the file. For bit n and ubit n precisions,

specifies the order for reading bits within a byte.

Specify machineformat to read and write to the file

on different systems, or to read parts of a byte in

a specific order.

Possible values are:

'n' or 'native' The byte ordering that your system

uses (default)

'b' or 'ieee-be' Big-endian ordering

'l' or 'ieee-le' Little-endian ordering

's' or

'ieee-be.l64' Big-endian ordering, 64-bit data type

'a' or

'ieee-le.l64' Little-endian ordering, 64-bit data type

Windows systems use little-endian ordering, and most

UNIX systems use big-endian ordering, for both bytes

and bits.

Output Arguments

A A column vector, unless you specify sizeA with the

form [m,n]. Data in A is class double unless you

specify a different class in the precision argument. count The number of elements that fread successfully reads.

Examples

Read the contents of a file:

% Create the file

fid = fopen('magic5.bin', 'w');

fwrite(fid, magic(5));

fclose(fid);

% Read the contents back into an array

fid = fopen('magic5.bin');

m5 = fread(fid, [5, 5], '*uint8');

fclose(fid);

Simulate the type function with fread, to display the contents of a text file:

fid = fopen('fread.m');

% read the entire file as characters

% transpose so that F is a row vector

F = fread(fid, '*char')'

fclose(fid);

If you do not specify the precision, fread applies the default

uint8=>double:

fid = fopen('fread.m');

F_nums = fread(fid, 6)' % read the first 6 bytes

('%FREAD')

fclose(fid);

This code returns

F_nums =

37 70 82 69 65 68

Read selected rows or columns from a file:

% Create a file with values from 1 to 9

fid = fopen('nine.bin', 'w');

alldata = reshape([1:9],3,3);

fwrite(fid, alldata);

fclose(fid);

% Read the first six values into two columns

fid = fopen('nine.bin');

two_cols = fread(fid, [3, 2]);

% Return to the beginning of the file

frewind(fid);

% Read two values at a time, skip one

% Returns six values into two rows

% (first two rows of 'alldata')

two_rows = fread(fid, [2, 3], '2*uint8', 1);

% Close the file

fclose(fid);

Specify machineformat to read separate digits of binary coded decimal (BCD) values correctly:

% Create a file with BCD values

str = ['AB'; 'CD'; 'EF'; 'FA'];

fid = fopen('bcd.bin', 'w');

fwrite(fid, hex2dec(str), 'ubit8');

fclose(fid);

% If you read one byte at a time,

% no need to specify machine format

fid = fopen('bcd.bin');

onebyte = fread(fid, 4, '*ubit8');

disp('Correct data, read with ubit8:')

disp(dec2hex(onebyte))

% However, if you read 4 bits on a little-endian

% system, your results appear in the wrong order frewind(fid); % return to beginning of file

part_err = fread(fid, 8, '*ubit4');

disp('Incorrect data on little-endian systems, ubit4:') disp(dec2hex(part_err))

% Specify a big-endian format for correct results frewind(fid);

part_corr = fread(fid, 8, '*ubit4', 'ieee-be');

disp('Correct result, ubit4:')

disp(dec2hex(part_corr))

fclose(fid);

fclose

Close one or all open files

Syntax

fclose(fileID)

fclose('all')

status = fclose(...)

Description

fclose(fileID) closes an open file. fileID is an integer file identifier obtained from fopen.

fclose('all') closes all open files.

status = fclose(...) returns a status of 0 when the close operation is successful. Otherwise, it returns -1.

MATLAB File Help: free Default Topics

free

--- help for xregpointer/free ---

free Frees heap which p points to

free(P) releases the memory that P is pointint to. Note that freeptr is called on https://www.doczj.com/doc/f97806207.html, as a destructor.

delete

Remove files or graphics objects

Alternatives

As an alternative to the delete function, use the Current Folder browser. Syntax

delete('fileName1', 'filename2', ...)

delete(h)

delete(handle_array)

delete fileName

Description

delete('fileName1', 'filename2', ...) deletes the files fileName1, fileName2, and so on, from the disk. fileName is a string and can be an absolute path or a path relative to the current folder. fileName also can include wildcards (*).

delete(h) deletes the graphics object with handle h. h can also be a vector or matrix of handles. Delete multiple objects by appending their handles as additional arguments, separated by commas. The function deletes objects without requesting verification, even if when they are windows.

delete(handle_array) is a method of the handle class. It removes from memory the handle objects referenced by handle_array.

When deleted, any references to the objects in handle_array become invalid. To remove the handle variables, use the clear function.

delete fileName is the command syntax. Delete multiple files by appending filenames, separated by spaces. When filenames contain space characters, you must use the functional form.

As delete does not ask for confirmation, to avoid accidentally losing files or graphics objects, make sure to specify accurately the items to delete. To move files to a different location when running delete, use the General preference for Deleting files, or the recycle function.

The delete function deletes files and graphics objects only. To delete folders, use rmdir.

Examples

Delete all files with a .mat extension in the ../mytests/ folder:

delete('../mytests/*.mat')

Create a figure and an axes, and then delete the axes:

hf = figure, ha = axes

hf =

1

ha =

170.0332

delete(ha)

The axes is deleted, but the figure remain. The axes handle ha remains in the workspace but no longer points to an object.

clear

Remove items from workspace, freeing up system memory

Syntax

clear

clear name

clear name1 name2 name3 ...

clear global name

clear -regexp expr1 expr2 ...

clear global -regexp expr1 expr2 ...

clear keyword

clear('name1','name2','name3',...)

Description

clear removes all variables from the workspace, releasing them from system memory.

clear name removes just the code file (function or script) or MEX-file function or variable name from your base workspace. If called from a function or script, clear name removes name from the workspace of just that function or script. You can use wildcards (*) to remove items selectively. For example, clear my* removes any variables whose names

begin with the string my. Clearing removes debugging breakpoints in code files and, when called from within a function with persistent variables, reinitializes the persistent variables. If name is global, clear removes it from the current workspace, but it remains accessible to any functions declaring it global. If name has been locked by mlock, it remains in memory.

Use a partial path to distinguish between different overloaded versions of a function. For example, clear polynom/display clears only the display method for polynom objects, leaving any other implementations in memory.

clear name1 name2 name3 ... removes name1, name2, and name3 from the workspace.

clear global name removes the global variable name. If name is global, clear name removes name from the current workspace, but leaves it accessible to any functions declaring it as global. Use clear global name to remove a global variable completely.

clear -regexp expr1 expr2 ... clears all variables that match any of the regular expressions listed. This option only clears variables.

clear global -regexp expr1 expr2 ... clears all global variables that match any of the regular expressions listed.

clear keyword clears the items indicated by keyword. See the following section, Input Arguments, for a list of keywords and their descriptions.

clear('name1','name2','name3',...) is the function form of the syntax. Use this form for variable names and function names stored in strings.

If you name a variable all, classes, functions, java, import, or variables, calling clear followed by that name deletes the variable with that name. clear does not interpret the name as a keyword in this context. For example, if the workspace contains variables a, all, b, and ball, clear all deletes the variable all only.

You can clear the handle of a figure or other object, but that does not remove the object itself. Use delete to remove objects and files. Deleting an object does not delete the variable, if any, used for storing its handle.

On UNIX systems, clear does not affect the amount of memory allocated to the MATLAB process.

The clear function does not clear Simulink models. Use close instead. Input Arguments

keyword A name specifying whether to clear variables,

classes, or packages or subsets of them, as described

in the following table:

matlab函数用法

A a abs 绝对值、模、字符的ASCII码值 acos 反余弦 acosh 反双曲余弦 acot 反余切 acoth 反双曲余切 acsc 反余割 acsch 反双曲余割 align 启动图形对象几何位置排列工具 all 所有元素非零为真 angle 相角 ans 表达式计算结果的缺省变量名 any 所有元素非全零为真 area 面域图 argnames 函数M文件宗量名 asec 反正割 asech 反双曲正割 asin 反正弦 asinh 反双曲正弦 assignin 向变量赋值 atan 反正切 atan2 四象限反正切 atanh 反双曲正切 autumn 红黄调秋色图阵 axes 创建轴对象的低层指令 axis 控制轴刻度和风格的高层指令 B b bar 二维直方图 bar3 三维直方图 bar3h 三维水平直方图 barh 二维水平直方图 base2dec X进制转换为十进制 bin2dec 二进制转换为十进制 blanks 创建空格串 bone 蓝色调黑白色图阵 box 框状坐标轴 break while 或for 环中断指令 brighten 亮度控制 C c

capture (3版以前)捕获当前图形 cart2pol 直角坐标变为极或柱坐标 cart2sph 直角坐标变为球坐标 cat 串接成高维数组 caxis 色标尺刻度 cd 指定当前目录 cdedit 启动用户菜单、控件回调函数设计工具cdf2rdf 复数特征值对角阵转为实数块对角阵ceil 向正无穷取整 cell 创建元胞数组 cell2struct 元胞数组转换为构架数组 celldisp 显示元胞数组内容 cellplot 元胞数组内部结构图示 char 把数值、符号、内联类转换为字符对象chi2cdf 分布累计概率函数 chi2inv 分布逆累计概率函数 chi2pdf 分布概率密度函数 chi2rnd 分布随机数发生器 chol Cholesky分解 clabel 等位线标识 cla 清除当前轴 class 获知对象类别或创建对象 clc 清除指令窗 clear 清除内存变量和函数 clf 清除图对象 clock 时钟 colorcube 三浓淡多彩交叉色图矩阵 colordef 设置色彩缺省值 colormap 色图 colspace 列空间的基 close 关闭指定窗口 colperm 列排序置换向量 comet 彗星状轨迹图 comet3 三维彗星轨迹图 compass 射线图 compose 求复合函数 cond (逆)条件数 condeig 计算特征值、特征向量同时给出条件数condest 范-1条件数估计 conj 复数共轭 contour 等位线 contourf 填色等位线 contour3 三维等位线

matlab中常见函数功用

⊙在matlab中clear,clc,clf,hold作用介绍 clear是清变量, clc只清屏, clf清除图形窗口上的旧图形, hold on是为了显示多幅图像时,防止新的窗口替代旧的窗口。 ①format:设置输出格式 对浮点性变量,缺省为format short. format并不影响matlab如何计算和存储变量的值。对浮点型变量的计算,即单精度或双精度,按合适的浮点精度进行,而不论变量是如何显示的。对整型变量采用整型数据。整型变量总是根据不同的类(class)以合适的数据位显示,例如,3位数字显示显示int8范围-128:127。 format short, long不影响整型变量的显示。 format long 显示15位双精度,7为单精度(scaled fixed point) format short 显示5位(scaled fixed point format with 5 digits) format short eng 至少5位加3位指数 format long eng 16位加至少3位指数 format hex 十六进制 format bank 2个十进制位 format + 正、负或零 format rat 有理数近似 format short 缺省显示 format long g 对双精度,显示15位定点或浮点格式,对单精度,显示7位定点或浮点格式。 format short g 5位定点或浮点格式 format short e 5位浮点格式 format long e 双精度为15位浮点格式,单精度为7为浮点格式 ②plot函数 基本形式 >> y=[0 0.58 0.70 0.95 0.83 0.25]; >> plot(y) 生成的图形是以序号为横坐标、数组y的数值为纵坐标画出的折线。 >> x=linspace(0,2*pi,30); % 生成一组线性等距的数值 >> y=sin(x); >> plot(x,y) 生成的图形是上30个点连成的光滑的正弦曲线。 多重线 在同一个画面上可以画许多条曲线,只需多给出几个数组,例如 >> x=0:pi/15:2*pi; >> y=sin(x); >> w=cos(x);

(完整版)MATLAB常用函数大全

一、MATLAB常用的基本数学函数 abs(x):纯量的绝对值或向量的长度 angle(z):复数z的相角(Phase angle) sqrt(x):开平方 real(z):复数z的实部 imag(z):复数z的虚部 conj(z):复数z的共轭复数 round(x):四舍五入至最近整数 fix(x):无论正负,舍去小数至最近整数 floor(x):地板函数,即舍去正小数至最近整数ceil(x):天花板函数,即加入正小数至最近整数rat(x):将实数x化为分数表示 rats(x):将实数x化为多项分数展开 sign(x):符号函数(Signum function)。 当x<0时,sign(x)=-1; 当x=0时,sign(x)=0; 当x>0时,sign(x)=1。 rem(x,y):求x除以y的馀数 gcd(x,y):整数x和y的最大公因数 lcm(x,y):整数x和y的最小公倍数 exp(x):自然指数 pow2(x):2的指数 log(x):以e为底的对数,即自然对数或 log2(x):以2为底的对数 log10(x):以10为底的对数 二、MATLAB常用的三角函数 sin(x):正弦函数 cos(x):余弦函数

tan(x):正切函数 asin(x):反正弦函数 acos(x):反馀弦函数 atan(x):反正切函数 atan2(x,y):四象限的反正切函数 sinh(x):超越正弦函数 cosh(x):超越馀弦函数 tanh(x):超越正切函数 asinh(x):反超越正弦函数 acosh(x):反超越馀弦函数 atanh(x):反超越正切函数 三、适用於向量的常用函数有: min(x): 向量x的元素的最小值 max(x): 向量x的元素的最大值 mean(x): 向量x的元素的平均值 median(x): 向量x的元素的中位数 std(x): 向量x的元素的标准差 diff(x): 向量x的相邻元素的差 sort(x): 对向量x的元素进行排序(Sorting)length(x): 向量x的元素个数 norm(x): 向量x的欧氏(Euclidean)长度sum(x): 向量x的元素总和 prod(x): 向量x的元素总乘积 cumsum(x): 向量x的累计元素总和cumprod(x): 向量x的累计元素总乘积 dot(x, y): 向量x和y的内积 cross(x, y): 向量x和y的外积 四、MATLAB的永久常数

matlab中的自定义函数与调用

Matlab自定义函数 1、函数文件+调用命令文件:需单独定义一个自定义函数的M文件; 2、函数文件+子函数:定义一个具有多个自定义函数的M文件; 3、Inline:无需M文件,直接定义; 4、Syms+subs:无需M文件,直接定义; 5、字符串+subs:无需M文件,直接定义. 6、匿名函数 7、直接通过@符号定义. 1、函数文件+调用函数文件:定义多个M文件: %调用函数文件:myfile.m clear clc for t=1:10 y=mylfg(t);%调用函数时要注意实参与形参的匹配! fprintf(‘%4d^(1/3)=%6.4f\n’,t,y); end %自定义函数文件:mylfg.m function y=mylfg(x)%注意:函数名(mylfg)必须与文件名(mylfg.m)一致 Y=x^(1/3); 注:这种方法要求自定义函数必须单独写一个M文件,不能与调用的命令文件写在同一个M文件中。 2、函数文件+子函数:定义一个具有多个子函数的M文件 %函数文件:funtry2.m function[]=funtry2()%可以无自变量()或无因变量[] for t=1:10 y=lfg2(t); fprintf('%4d^(1/3)=%6.4f\n',t,y); end function y=lfg2(x)%%子函数 y=x^(1/3);

%注:自定义函数文件funtry2.m中可以定义多个子函数function。子函数lfg2只能被主函数和主函数中的其他子函数调用。 3、Inline:无需M文件,直接定义; %inline命令用来定义一个内联函数:f=inline(‘函数表达式’,‘变量1’,’变量2’,……)。 调用方式:y=f(数值列表)%注意:代入的数值列表顺序应与inline()定义的变量名顺序一致。 例如: f=inline(‘x^2+y’,’x’,’y’); z=f(2,3) Ans=7 注:这种函数定义方式是将它作为一个内部函数调用。特点是,它是基于Matlab的数值运算内核的,所以它的运算速度较快,程序效率更高。缺点是,该方法只能对数值进行代入,不支持符号代入,且对定义后的函数不能进行求导等符号运算。 内联函数定义方式是将f作为一个内部函数调用。其特点是:调用方式最接近于我们平时对函数的定义,使程序更具可读性。同时由于它是基于Matlab的数值计算内核的,所以它的运算速度较快,程序更有效率。 这种定义方式的缺点: 定义一个内联函数用去的内存空间比相同条件下其他的方法要大得多。 该方法只能对数值进行代入,不支持符号代入,并且对于定义后的函数不能进行求导等符号运算。 例:通过命令clear清除工作空间的所有变量后,执行如下指令 Clear Clc f=’x^2’; Syms x g; g=x^2; h=inline(‘x^2’,’x’); whos 4、Syms+subs:无需M文件,直接定义; 用syms定义一个符号表达式,用subs调用: Syms f x%定义符号 f=1/(1+x^2);%定义符号表达式也是符号

(完整版)matlab函数大全(非常实用)

信源函数 randerr 产生比特误差样本 randint 产生均匀分布的随机整数矩阵 randsrc 根据给定的数字表产生随机矩阵 wgn 产生高斯白噪声 信号分析函数 biterr 计算比特误差数和比特误差率 eyediagram 绘制眼图 scatterplot 绘制分布图 symerr 计算符号误差数和符号误差率 信源编码 compand mu律/A律压缩/扩张 dpcmdeco DPCM(差分脉冲编码调制)解码dpcmenco DPCM编码 dpcmopt 优化DPCM参数 lloyds Lloyd法则优化量化器参数 quantiz 给出量化后的级和输出值 误差控制编码 bchpoly 给出二进制BCH码的性能参数和产生多项式convenc 产生卷积码 cyclgen 产生循环码的奇偶校验阵和生成矩阵cyclpoly 产生循环码的生成多项式 decode 分组码解码器 encode 分组码编码器 gen2par 将奇偶校验阵和生成矩阵互相转换gfweight 计算线性分组码的最小距离 hammgen 产生汉明码的奇偶校验阵和生成矩阵rsdecof 对Reed-Solomon编码的ASCII文件解码rsencof 用Reed-Solomon码对ASCII文件编码rspoly 给出Reed-Solomon码的生成多项式syndtable 产生伴随解码表 vitdec 用Viterbi法则解卷积码 (误差控制编码的低级函数) bchdeco BCH解码器 bchenco BCH编码器 rsdeco Reed-Solomon解码器 rsdecode 用指数形式进行Reed-Solomon解码 rsenco Reed-Solomon编码器 rsencode 用指数形式进行Reed-Solomon编码 调制与解调

Matlab 图像处理相关函数命令大全

Matlab 图像处理相关函数命令大全 一、通用函数: colorbar 显示彩色条 语法:colorbar \ colorbar('vert') \ colorbar('horiz') \ colorbar(h) \ h=colorbar(...) \ colorbar(...,'peer',axes_handle) getimage 从坐标轴取得图像数据 语法:A=getimage(h) \ [x,y,A]=getimage(h) \ [...,A,flag]=getimage(h) \ [...]=getimage imshow 显示图像 语法:imshow(I,n) \ imshow(I,[low high]) \ imshow(BW) \ imshow(X,map) \ imshow(RGB)\ imshow(...,display_option) \ imshow(x,y,A,...) \ imshow filename \ h=imshow(...) montage 在矩形框中同时显示多幅图像 语法:montage(I) \ montage(BW) \ montage(X,map) \ montage(RGB) \ h=montage(...) immovie 创建多帧索引图的电影动画 语法:mov=immovie(X,map) \ mov=immovie(RGB) subimage 在一副图中显示多个图像 语法:subimage(X,map) \ subimage(I) \ subimage(BW) \ subimage(RGB) \ subimage(x,y,...) \ subimage(...) truesize 调整图像显示尺寸 语法:truesize(fig,[mrows mcols]) \ truesize(fig)

Matlab与51单片机的串口通信

数字信号处理2012电子信息工程专业答辩报告

Matlab与51单片机的串口通信 一、简介 从Matlab6.0版本开始,Mathworks公司在软件中增加了设备控制箱(instrument control toolbox),提供了对RS-232/RS-485通信标准串口(九针串口)通信正式支持(本实验采用USB转串口)利用该工具箱serial类及instrcallback()回调函数,能可靠地进行实时串地通信。Matlab支持面向对象技术,用一个对象将计算机串口封装起来,只要创建串口对象,对串口对象操作就是对串口操作,非常方便。使用serial函数就可创建串口对象,通过定义串口对象的属性,能定义串口的通信模式,从串口对象属性也能了解串口的状态,即可以通过MATLAB的串口通讯函数读写数据。 二、 Matlab串口函数 serial 创建一个串口对象,格式:s = serial('coml' ) fopen 打开串口对象,格式:fopen(s) fwrite 其他程序能对该串口进行读写操作fwrite(s,’’) fread 读取串口数据,格式: fread(s) fclose 关闭串口对象,格式:fclose(s) free 解除Matlab对串口对象的控制,使 delete 删除对象s,格式:delete(s) clear 从工作空间中删除对象s,格式:clear(s) 三、实现功能 利用MATLAB串口通信函数,读写51单片机(STC89C52R+)数据,运用keil编写时钟程序,烧录到单片机中,时钟程序实现的功能是实现时钟的显示,并且能用开发板上的三个按钮进行时钟的修改,一个按钮进入修改模式(复位),另两个实现时间的增减。编辑MATLAB程序,实现对单片机的控制。读写串口操作。初始化并打开串口调协对象之后,现在可以对串口设备对象进行读写操作,串口读写操作支持二进制和文本(ASCII)两种方式。当Matlab通信数据采用西方(ASCII)方式时,读写串口设备命令分别是fscanf、fpritf;当Matlab通信数据采用二进制方式时,读写串口设备命令分别是fread、fwrite。

自相关函数和互相关函数的利用MATLAB计算和作图

互相关函数,自相关函数计算和作图 1.自相关和互相关的概念。 ●互相关函数是描述随机信号x(t),y(t)在任意两个不同时刻t1,t2间的相关程度。 ●自相关函数是描述随机信号x(t)在任意两个不同时刻t1,t2间的相关程度。 互相关函数是在频域内两个信号是否相关的一个判断指标,把两测点之间信号的互谱与各自的自谱联系了起来。它能用来确定输出信号有多大程度来自输入信号,对修正测量中接入噪声源而产生的误差非常有效。 -----------------------------------------------------------------------------------事实上,在图象处理中,自相关和互相关函数的定义如下:设原函数是f(t),则自相关函数定义为R(u)=f(t)*f(-t),其中*表示卷积;设两个函数分别是f(t)和g(t),则互相关函数定义为R(u)=f(t)*g(-t),它反映的是两个函数在不同的相对位置上互相匹配的程度。 2.利用matlab中实现这两个相关并用图像显示: 自相关函数: dt=.1; t=[0:dt:100];x=cos(t); [a,b]=xcorr(x,'unbiased'); plot(b*dt,a)

互相关函数:把[a,b]=xcorr(x,'unbiased');改为[a,b]=xcorr(x,y,'unbiased');便可。 3.实现过程: 在Matalb中,求解xcorr的过程事实上是利用Fourier变换中的卷积定理进行的,即 R(u)=ifft(fft(f)×fft(g)),其中×表示乘法,注:此公式仅表示形式计算,并非实际计算所用的公式。当然也可以直接采用卷积进行计算,但是结果会与xcorr的不同。事实上,两者既然有定理保证,那么结果一定是相同的,只是没有用对公式而已。下面是检验两者结果相同的代码: dt=.1; t=[0:dt:100]; x=3*sin(t); y=cos(3*t); subplot(3,1,1); plot(t,x); subplot(3,1,2); plot(t,y); [a,b]=xcorr(x,y); subplot(3,1,3); plot(b*dt,a); yy=cos(3*fliplr(t));%or use:yy=fliplr(y); z=conv(x,yy); pause; subplot(3,1,3); plot(b*dt,z,'r'); 即在xcorr中不使用scaling。

MATLAB各种“窗函数”定义及调用

MATLAB窗函数大全 1.矩形窗(Rectangle Window)调用格式:w=boxcar(n),根据长度n 产生一个矩形窗w。 2.三角窗(Triangular Window)调用格式:w=triang(n),根据长度n 产生一个三角窗w。 3.汉宁窗(Hanning Window)调用格式:w=hanning(n),根据长度n 产生一个汉宁窗w。 4.海明窗(Hamming Window)调用格式:w=hamming(n),根据长度n 产生一个海明窗w。 5.布拉克曼窗(Blackman Window)调用格式:w=blackman(n),根据长度n 产生一个布拉克曼窗w。 6.恺撒窗(Kaiser Window)调用格式:w=kaiser(n,beta),根据长度n 和影响窗函数旁瓣的β参数产生一个恺撒窗w。 窗函数: 1.矩形窗:利用w=boxcar(n)的形式得到窗函数,其中n为窗函数的长度,而返回值w为一个n阶的向量,它的元素由窗函数的值组成。‘w=boxcar(n)’等价于‘w=ones(1,n)’. 2.三角窗:利用w=triang(n)的形式得到窗函数,其中n为窗函数的长度,而返回值w为一个n阶的向量,它的元素由窗函数的值组成。 w=triang(N-2)等价于bartlett(N)。

3.汉宁窗:利用w=hanning(n)得到窗函数,其中n为窗函数的长度,而返回值w 为一个n 阶的向量,包含了窗函数的n个系数。 4.海明窗:利用w=hamming(n)得到窗函数,其中n为窗函数的长度,而返回值w 为一个n 阶的向量,包含了窗函数的n个系数。它和汉宁窗的主瓣宽度相同,但是它的旁瓣进一步被压低。 5.布拉克曼窗:利用w=blackman(n)得到窗函数,其中n为窗函数的长度,而返回值w为一个n阶的向量,包含了窗函数的n个系数。它的主瓣宽度是矩形窗主瓣宽度的3倍,为12*pi/N,但是它的最大旁瓣值比主瓣值低57dB。 6.切比雪夫窗:它是等波纹的,利用函数w=chebwin(N,R)方式设计出N阶的切比雪夫2窗函数,函数的主瓣值比旁瓣值高RdB,且旁瓣是等波纹的。 7.巴特里特窗:利用w=bartlett(n)的形式得到窗函数,其中n为窗函数的长度,而返回值w为一个n阶的向量,包含了窗函数的n个系数。 8.凯塞窗:利用w=kaiser(n,beta)的形式得到窗函数。

matlab各种函数的用法

1 Text函数的用法: 用法 text(x,y,'string')在图形中指定的位置(x,y)上显示字符串string text(x,y,z,'string') 在三维图形空间中的指定位置(x,y,z)上显示字符串string 2, plot([0,z1,z12],'-b','LineWidth',3)[ ]里面表示数组. 3, x,y均为矩阵,plot命令就是画出x,y矩阵对应的二维平面的点形成的曲线。y(:,1)中逗号前是行,逗号后是列,冒号表示从几到几。所以y(:,1)表示第一列的所有元素。如果是y(3:5,1)则表示第一列的第3到第5行对应的元素。只要你的y矩阵有100列,那你当然可以将1改成100。同理,x矩阵也可以这样。 4 sym的意思是symbol,就是后面括号里面是个代数式,要进行符号运算,class()判断对象是什么类型。 5 matlab控制运算精度用的是digits和vpa这两个函数 xs = vpa(x,n) 在n位相对精度下,给出x的数值型符号结果xs xs = vpa(x) 在digits指定的精度下,给出x的数值型符号结果xs

digits用于规定运算精度,比如: digits(20); 这个语句就规定了运算精度是20位有效数字。但并不是规定了就可以使用,因为实际编程中,我们可能有些运算需要控制精度,而有些不需要控制。vpa就用于解决这个问题,凡是用需要控制精度的,我们都对运算表达式使用vpa函数。例如: digits(5); a=vpa(sqrt(2)); 这样a的值就是1.4142,而不是准确的1.4880 又如: digits(5); a=vpa(sqrt(2)); b=sqrt(2); 这样a的值是1.4142,b没有用vpa函数,所以b是1.4880...... 6

matlab串口实时波形显示

作者:GG 功能:实现matalb与PC外设通讯 本例:串口232与外设单片机51通讯。实时监控51数据并且实时图形显示 时间:2011—9—16 简介:实现该功能使用M脚本文件和函数文件。 第一个文件连接串口和打开串口,设置了串口的一些参数和触发事件。连接串口COM5。有关该方面的知识请自行百度I/O文字流。 第二个文件是时间回调函数,相当于其他语言中例如C语言的中断函数 第三文件是关闭串口和清除列连接。并且清除中间TXT中介文件内容 下面是源文件 第一个: clear all s=serial('COM5');%打开串口 s.BytesAvailableFcnMode='byte';%设置事件触发为接受触发 s.InputBufferSize=5000;%设置接受缓冲区大小为5000个字节 s.BytesAvailableFcnCount=10;%每次接受到50个数据时候触发事件 s.BaudRate=19200;%设置通讯波特率 s.BytesAvailableFcn=@my_callback;%指向触发事件函数 fopen(s);%打开串口 第二个 function my_callback(obj,event) out=fread(obj,10,'uint8');%串口处读出50个数据 fid=fopen('G1.txt','a+');%打开文件并且追加 fprintf(fid,'%3d',out); fclose(fid); speed=textread('G1.txt','%u'); plot(speed); disp('save ok!'); end 第三个 fclose(s);%关闭串口 delete(s);%删除串口变量 clear all; fid=fopen('G1.txt','w');%清除中间文件txt a=[]; fprintf(fid,'%s',a); fclose(fid); clear all;%清除所以变量

matlab求两个序列的互相关函数

%----------------------------------------------------------------- % exa011007_xcorr.m: for example 1.10.7 and example 1.8.3 % to test xcorr.m % 求两个序列的互相关函数,或一个序列的自相关函数;%----------------------------------------------------------------- clear; N=500; p1=1; p2=0.1; f=1/8; Mlag=50; u=randn(1,N); n=[0:N-1]; s=sin(2*pi*f*n); % 混有高斯白噪的正弦信号的自相关 u1=u*sqrt(p1); x1=u1(1:N)+s; rx1=xcorr(x1,Mlag,'biased'); subplot(221); plot(x1(1:Mlag)); xlabel('n'); ylabel('x1(n)');grid on; subplot(223); plot((-Mlag:Mlag),rx1);grid on;

xlabel('m');ylabel('rx1(m)'); % 高斯白噪功率由原来的p1减少为p2,再观察混合信号的自相关 u2=u*sqrt(p2); x2=u2(1:N)+s; rx2=xcorr(x2,Mlag,'biased'); subplot(222); plot(x2(1:Mlag)); xlabel('n');ylabel('x2(n)');grid on; subplot(224); plot((-Mlag:Mlag),rx2); grid on;xlabel('m');ylabel('rx2(m)');

matlab基本函数的用法

一. Matlab中常见函数基本用法 1.sum (1 )sum(A)A为矩阵得出A矩阵每列的和组成的一个矢量; A为矢量得出A的各元 素之和 (2)sum(diag(A))得矩阵A的对角元素之和 (3)sum(A,dim) A为矩阵,sum(A,1)按列求和;sum(A,2)按行求和 2.max(min) (1)max(A) 若A为矩阵则得出A矩阵每列的最大元素组成的一个矢量 若A为矢量则得出A中最大的元 (2)max(A,B) A与B为同维矩阵得出取A 与B中相同位置元素中较大者组成的新矩阵 (3)max(A,[],dim) max(a,[ ],1),求每列的最大值;max(a,[ ],2)求每行的最大值 3.find (1)find(X)若X为行向量则得出X中所有非零元素所在的位置(按行)若X为列向量或矩阵则得出X中所有非零元素的位置(按列)(2)ind = find(X, k)/ind = find(X,k,'first') 返回前k个非零元的指标ind = find(X,k,'last') 返回后k个非零元的指标 (3)[row,col] = find(X) row代表行指标,col代表列指标 [row,col,val] = find(X) val表示查找到对应位置非零元的值 [row,col] = find(A>100 & A<1000) 找出满足一定要求的元素 4.reshape (1)B = reshape(A,m,n) 把A变成m*n的矩阵 5.sort (1)B = sort(A) 把A的元素按每列从小到大的顺序排列组成新矩阵

(2)B = sort(A,dim) dim=1同(1); dim=2 把A按每行从小到大的顺序排列组成新矩阵 6.cat (1)C = cat(dim, A, B) dim=1相当于[A;B];dim=2相当于[A,B] (2)C = cat(dim, A1, A2, A3, A4, ...) 类推(1) 7.meshgrid (1)[X,Y] = meshgrid(x,y) 将向量x和y定义的区域转换成矩阵X和Y,矩阵X的行向量是向量x的简单复制,而矩阵Y的列向量是向量y的简单复制。(2)[X,Y] = meshgrid(x) (1)y=x中情形 8.diag (1)X = diag(v,k) 向量v作为X的第k对角线上的元素X的其他元素为零(2)X = diag(v) (1)中k=0的情况 (2)v = diag(X,k) v为矩阵X的第k对角线的元素组成的列向量 (4)v = diag(X) (3)中k等于零的情况

matlab串口通信

摘要:结合单片机和Matlab两者优点,基于事件驱动中断通信机制,提出一种Matlab环境下PC机与单片机实时串行通信及数据处理方法;完成单片机数据采集系统与PC机RS-232/RS-485串行通信及其通信数据分析处理、文件存储、FIR滤波及图形显示;简化系统开发流程,提高开发效率。该方法已成功应用于一个PIC16F876单片机应用系统实例之中。 关键词:PIC16F876 Matlab 串口通信 RS-232 事件驱动回调函数 引言 Matlab是由美国Mathworks公司开发面向理论分析研究、工程计算数据处理和缓图一套具有强大功能软件系统。其中Matlab语言是一种以矩阵为基本运算单元解释执行高级语言,编程简例,只要几条语句就能实现诸如FFT变换、FIR/IIR滤波等数据分析处理,易于掌握。从Matlab6.0版本开始,Mathworks 公司在软件中增加了设备控制箱(instrument control toolbox),提供了对RS-232/RS-485通信标准串口通信正式支持。利用该工具箱serial类及instrcallback()回调函数,能可靠地进行实时串地通信。为此,笔者充分结合单片机和Matlab优点,基于事件驱动中断通信机制,提出了一种Matlab环境下PC机与单片机实时串行通信数据处理方法,极大地简化开发流程,提高了系统开发效率。另外,与目前普遍采用基于Matlab查询方式下非实时串行通信技术相比,这种方法实用性也大大增强了。 https://www.doczj.com/doc/f97806207.html,提示请看下图: 1 系统总体设计简介 下面以Mircochip公司PIC16F876单片机为下位机,PC机为上位机组成实时数据采集处理系统为例,介绍基于Matlab环境下PC机与单片机串行通信实时数据处理方法实现。数据采集系统结构框图如图1所示。PC机串口与单片机USART 口通过MAX232电平转换芯片相连,系统工作时,Matlab通过调用设备控制工具箱中serial类及相关函数。来创建串口设备对象,得到设备文件句柄,从而以操作文件方式实现对PC机串行口读写操作。因而PC机可以通过Matlab向串行

MATLAB图像处理相关函数

一、通用函数: colorbar显示彩色条 语法:colorbar \ colorbar('vert') \ colorbar('horiz') \ colorbar(h) \ h=colorbar(...) \ colorbar(...,'peer',axes_handle) getimage 从坐标轴取得图像数据 语法:A=getimage(h) \ [x,y,A]=getimage(h) \ [...,A,flag]=getimage(h) \ [...]=getimage imshow 显示图像 语法:imshow(I,n) \ imshow(I,[low high]) \ imshow(BW) \ imshow(X,map) \ imshow(RGB)\ imshow(...,display_option) \ imshow(x,y,A,...) \ imshow filename \ h=imshow(...) montage 在矩形框中同时显示多幅图像 语法:montage(I) \ montage(BW) \ montage(X,map) \ montage(RGB) \ h=montage(...) immovie 创建多帧索引图的电影动画 语法:mov=immovie(X,map) \ mov=immovie(RGB) subimage 在一副图中显示多个图像 语法:subimage(X,map) \ subimage(I) \ subimage(BW) \ subimage(RGB) \ subimage(x,y,...) \ subimage(...) truesize 调整图像显示尺寸 语法:truesize(fig,[mrows mcols]) \ truesize(fig) warp 将图像显示到纹理映射表面 语法:warp(X,map) \ warp(I ,n) \ warp(z,...) warp(x,y,z,...) \ h=warp(...) zoom 缩放图像 语法:zoom on \ zoom off \ zoom out \ zoom reset \ zoom \ zoom xon \ zoom yon\ zoom(factor) \ zoom(fig,option) 二、图像文件I/O函数命令 imfinfo 返回图形图像文件信息 语法:info=imfinfo(filename,fmt) \ info=imfinfo(filename) imread 从图像文件中读取(载入)图像 语法:A=imread(filename,fmt) \ [X,map]=imread(filename,fmt) \

Matlab中的函数

abs 绝对值、模、字符的ASCII码值? acos 反余弦? acosh 反双曲余弦? acot 反余切? acoth 反双曲余切? acsc 反余割? acsch 反双曲余割? align 启动图形对象几何位置排列工具? all 所有元素非零为真? angle 相角? ans 表达式计算结果的缺省变量名? any 所有元素非全零为真? area 面域图? argnames 函数M文件宗量名? asec 反正割? asech 反双曲正割? asin 反正弦? asinh 反双曲正弦? assignin 向变量赋值? atan 反正切? atan2 四象限反正切? atanh 反双曲正切? autumn 红黄调秋色图阵? axes 创建轴对象的低层指令? axis 控制轴刻度和风格的高层指令? B b? bar 二维直方图? bar3 三维直方图? bar3h 三维水平直方图? barh 二维水平直方图? base2dec X进制转换为十进制? bin2dec 二进制转换为十进制? blanks 创建空格串? bone 蓝色调黑白色图阵? box 框状坐标轴?

break while 或for 环中断指令? brighten 亮度控制? C c? capture (3版以前)捕获当前图形? cart2pol 直角坐标变为极或柱坐标? cart2sph 直角坐标变为球坐标? cat 串接成高维数组? caxis 色标尺刻度? cd 指定当前目录? cdedit 启动用户菜单、控件回调函数设计工具? cdf2rdf 复数特征值对角阵转为实数块对角阵? ceil 向正无穷取整? cell 创建元胞数组? cell2struct 元胞数组转换为构架数组? celldisp 显示元胞数组内容? cellplot 元胞数组内部结构图示? char 把数值、符号、内联类转换为字符对象? chi2cdf 分布累计概率函数? chi2inv 分布逆累计概率函数? chi2pdf 分布概率密度函数? chi2rnd 分布随机数发生器? chol Cholesky分解? clabel 等位线标识? cla 清除当前轴? class 获知对象类别或创建对象? clc 清除指令窗? clear 清除内存变量和函数? clf 清除图对象? clock 时钟? colorcube 三浓淡多彩交叉色图矩阵? colordef 设置色彩缺省值? colormap 色图? colspace 列空间的基? close 关闭指定窗口? colperm 列排序置换向量?

MATLAB串口通信

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%本程序主要实现串口控制三轴转台进行自动标定,%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%查找串口对象,若串口一开始被占用,需要加上这段程序释放串口,若串口没有被占用,则不需要这段程序 scoms=instrfind; %%尝试停止、关闭删除串口对象 stopasync(scoms); fclose(scoms);%关闭串口 delete(scoms);%释放串口%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% clear all;clc; %%%%%串口配置通道1 global s; s=serial('COM13');%设置串口号 s.baudrate=57600;%设置波特率 s.parity='none';%校验位 s.stopbits=1;%停止位 s.databits=7;%数据位 s.timeout=2;%一次读/写的时间最长为0.5s s.InputBufferSize=1024;%输入缓冲区 s.OutputBufferSize=1024;%输出缓冲区 %s.BytesAvailableFcnMode='byte';%中断触发事件为'bytes-available event' %s.BytesAvailableFcnCount=8;%接收缓冲区每收到n个字节时,触发回调函数%s.BytesAvailableFcn={'cmd_rec_callback',handles};%得到回调函数句柄fopen(s);%%%%打开串口 fclose(s); %%%%%串口配置通道2 global s2; s2=serial('COM15');%设置串口号 s2.baudrate=460800;%设置波特率 s2.parity='none';%校验位 s2.stopbits=1;%停止位 s2.databits=8;%数据位 s2.timeout=2;%一次读/写的时间最长为0.5s s2.InputBufferSize=1024;%输入缓冲区 s2.OutputBufferSize=1024;%输出缓冲区 %s.BytesAvailableFcnMode='byte';%中断触发事件为'bytes-available event' %s.BytesAvailableFcnCount=8;%接收缓冲区每收到n个字节时,触发回调函数%s.BytesAvailableFcn={'cmd_rec_callback',handles};%得到回调函数句柄fopen(s2);%%%%打开串口 fclose(s2); %InitPos(1)=InitPos(1)+0.3 %Pos1=num2str(InitPos(1)'); %Pos1=['Q010',Pos1(1,:),'$']; %A=[00000000000]; %fwrite(s2,A,'uint8') %AA=fread(s2,11,'uint8')

Matlab自相关函数和互相关函数的计算和作图

自相关函数(Autocorrelation function,缩写ACF)是信号处理、时间序列分析中常用的数学工具,反映了同一序列在不同时刻的取值之间的相关程度。 自相关函数在不同的领域,定义不完全等效。在某些领域,自相关函数等同于自协方差(autocovariance)。 信号处理 在信息分析中,通常将自相关函数称之为自协方差方程。用来描述信息在不同时间τ的,信息函数值的相关性。 ,其中“*”是卷积算符,为取共轭 自相关函数的性质 以下以一维自相关函数为例说明其性质,多维的情况可方便地从一维情况推广得到。 ?对称性:从定义显然可以看出R(i) = R(?i)。连续型自相关函数为偶函数当f为实函数时,有: 当f是复函数时,该自相关函数是厄米函数,满足: 其中星号表示共轭。 ?连续型实自相关函数的峰值在原点取得,即对于任何延时τ,均有 。该结论可直接有柯西-施瓦茨不等式得到。离散型自相关函数亦有此结论。 ?周期函数的自相关函数是具有与原函数相同周期的函数。 ?两个相互无关的函数(即对于所有τ,两函数的互相关均为0)之和的自相关函数等于各自自相关函数之和。 ?由于自相关函数是一种特殊的互相关函数,所以它具有后者的所有性质。

?连续时间白噪声信号的自相关函数是一个δ函数,在除τ = 0 之外的所有点均为0。 ?维纳-辛钦定理(Wiener–Khinchin theorem)表明,自相关函数和功率谱密度函数是一对傅里叶变换对: ?实值、对称的自相关函数具有实对称的变换函数,因此此时维纳-辛钦定理中的复指数项可以写成如下的余弦形式: 白噪声的自相关函数为δ函数: 自相关函数和偏相关函数的问题 在时间序列分析的研究中,首先是判别时间序列的稳定性,如果时间序列是平稳的就可以计算这些数据的自相关函数和偏相关函数。 如果自相关函数是拖尾的,偏相关函数是截尾的,那麽数据符合AR(P)模型。 如果自相关函数是截尾的,偏相关函数是拖尾的,那麽数据复合MA( Q )模型 如果自相关函数和偏相关函数都是拖尾的,那麽数据复合ARMA( P,Q )模型。 自相关函数和互相关函数的matlab计算和作图 1. 首先说说自相关和互相关的概念。 这个是信号分析里的概念,他们分别表示的是两个时间序列之间和同一个时间序列在任意两个不同时刻的取值之间的相关程度,即互相关函数是描述随机信号x(t),y(t)在任意两个不同时刻t1,t2的取值之间的相关程度,自相关函数是描述随机信号x(t)在任意两个不同时刻t1,t2的取值之间的相关程度。互相关函数给出了在频域内两个信号是否相关的一个判断指标,把两测点之间信号的互谱与

相关主题
文本预览
相关文档 最新文档