您的位置:首页 > 编程语言 > MATLAB

matlab

2016-01-26 15:36 597 查看








hold on 是当前轴及图形保持而不被刷新,准备接受此后将绘制

hold off 使当前轴及图形不在具备被刷新的性质

>> t=(0:pi/100:pi)';

y1=sin(t)*[1,-1];

y2=sin(t).*sin(9*t);

t3=pi*(0:9)/9;

y3=sin(t3).*sin(9*t3);

plot(t,y1,'r:',t,y2,'-bo')

hold on

plot(t3,y3,'s','MarkerSize',10,'MarkerEdgeColor',[0,1,0],'MarkerFaceColor',[1,0.8,0])

axis([0,pi,-1,1])

hold off

>> 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为横轴y为纵轴

多重线

在同一个画面上可以画许多条曲线,只需多给出几个数组,例如

>> x=0:pi/15:2*pi;
>> y1=sin(x);
>> y2=cos(x);
>> plot(x,y1,x,y2)

则可以画出多重线。另一种画法是利用hold命令。在已经画好的图形上,若设置hold on,MATLA将把新的plot命令产生的图形画在原来的图形上。而命令hold off 将结束这个过程。例如:

>> x=linspace(0,2*pi,30); y=sin(x); plot(x,y)
>> hold on
>> z=cos(x); plot(x,z)
>> hold off


在MATLAB中,可以使用sprintf来格式化输出变量。

MATLAB的sprintf用法几乎和C中的printf一样,参数都是printf(FORMAT,A,...)

MATLAB的sprintf会返回一个字符串,当不使用分号;时,可以直接在屏幕输出。

MATLAB的sprintf语法为[s, errmsg] = sprintf(format, A, ...)

可以把矩阵A做数据格式的转换,格式就是format参数。

例子:

Str = [65 66 67 pi];

sprintf('%s %f', Str)

可得:

ans =

ABC 3.141593

matlab设计算法很简单,但是在c上去实现就比较麻烦了,采用matlab自带的mcc链接工具可以方便快速地将需要的m
文件转换为c文件。将m文件转换为c文件有一个很重要的地方需要注意:m文件的开头必须是function[输出参数1,输出参数2,...]=m文件名(输入参数1,输入参数2.....)。否则的话,会提示"...is a script M-file and cannot be compiled with the current Compiler."

实例,将以下语句编写为m文件保存。
function[yfft]=tstmcc( );

y=[1,2,3,5,8];

yfft=fft(y,100);

在matlab下执行mcc -t -l tstmcc.m即可生成c和h文件。

FDA设计

function Hd = passband_fixed_20
%PASSBAND_FIXED_20 Returns a discrete-time filter object.

% MATLAB Code
% Generated by MATLAB(R) 8.2 and the Signal Processing Toolbox 6.20.
% Generated on: 29-Feb-2016 16:10:01

% Butterworth Bandpass filter designed using FDESIGN.BANDPASS.

% All frequency values are in Hz.
Fs = 10000;  % Sampling Frequency

Fstop1 = 10;          % First Stopband Frequency
Fpass1 = 15;          % First Passband Frequency
Fpass2 = 25;          % Second Passband Frequency
Fstop2 = 30;          % Second Stopband Frequency
Astop1 = 60;          % First Stopband Attenuation (dB)
Apass  = 1;           % Passband Ripple (dB)
Astop2 = 80;          % Second Stopband Attenuation (dB)
match  = 'stopband';  % Band to match exactly

% Construct an FDESIGN object and call its BUTTER method.
h  = fdesign.bandpass(Fstop1, Fpass1, Fpass2, Fstop2, Astop1, Apass, ...
Astop2, Fs);
Hd = design(h, 'butter', 'MatchExactly', match);


clear;
clc;
close all;
fs = 10000;
t = 0 : 1/fs : 1;
f0 = 10;
signal = sin(2 * pi * f0 * t) + sin(2 * pi * 2 * f0 * t) +sin(2 * pi * 3 * f0 * t);
Hd = passband_fixed_20;
signal_filtered = filter(Hd, signal);
plot(t, sin(2 * pi * 2 * f0 * t), 'r', t, signal_filtered, 'g');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: