数字信号处理(MATLAB入门例子)

ops/2024/9/24 21:20:32/

(代码主要来源于这本书)

1.用MATLAB产生32个正弦波样本,A=2,f=1000Hz,以及fs=8000Hz

%
% Example 2.1 Sinewave generator
% This example generate 32 sine sample,
% plot it and save in sine.dat file% For the book "Real Time Digital Signal Processing: 
%               Fundamentals, Implementation and Application, 3rd Ed"
%               By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
%               Publisher: John Wiley and Sons, Ltdn = [0:31];             % Time index n
omega = 0.25*pi;        % Digital frequency
xn = 2*sin(omega*n);    % Sinewave generation
plot(n, xn, '-o');      % Samples are marked by 'o'
xlabel('Time index, n'); 
ylabel('Amplitude');
axis([0 31 -2 2]);		% Define ranges of plot
save sine.dat xn -ascii;% Save in ASCII data file

例2:绘制传递函数的零极点图:

% Example 2.11a Mgnitude and phase response of an IIR filter
% This example plots magnitude and phase response of an IIR filter% For the book "Real Time Digital Signal Processing: 
%               Fundamentals, Implementation and Application, 3rd Ed"
%               By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
%               Publisher: John Wiley and Sons, Ltdb=[1];          % Define numerator
a=[1, -1, 0.9]; % Denominator 
zplane(b,a);    % Pole-zero plot

例3:正弦波A=1,f=1000Hz,采样率为 10 000Hz,可以产生100个正弦波采样样本。信号的幅度谱可以采用以下MATLAB程序绘制:

% example2_16.m - Compute and plot amplitude spectrum of sinewave
%% For the book "Real Time Digital Signal Processing: 
%               Fundamentals, Implementation and Application, 3rd Ed"
%               By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
%               Publisher: John Wiley and Sons, LtdN=100; f = 1000; fs = 10000; % Define parameter values
n=[0:N-1]; k=[0:N-1];        % Define time and frequency indices
omega=2*pi*f/fs;             % Frequency of sinewave
xn=sin(omega*n);             % Generate sinewave
Xk=fft(xn,N);                % Perform DFT
magXk=20*log10(abs(Xk));     % Compute magnitude spectrum
plot(k, magXk); axis([0, N/2, -inf, inf]); % plot from 0 to pi
xlabel('Frequency index, k');
ylabel('Magnitude (dB)');

例4:绘制下面这个传递函数的幅度响应和相位响应

% For the book "Real Time Digital Signal Processing: 
%               Fundamentals,Implementation and Application, 3rd Ed"
%               By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
%               Publisher: John Wiley and Sons, Ltdb=[1, 0, 0, 0, 0, 0, 0, 0, -1]; % Numerator
a=[1, -1];                      % Denominator
freqz(b,a);                     % Plot frequency response

例5:本例产生中的一个包含噪声的正弦波信号,其中噪声是零均值、单位方差白噪声所示。


N=256; A=sqrt(2); w0=0.2*pi;   % Define parameters
n = [0:N-1];                   % Time index
sn = A*sin(w0*n);              % Sine sequence
sd = 12357;                    % Define seed value
rng(sd);                       % Use defined seed
vn = (rand(1,N)-0.5)*sqrt(12); % Zero-mean, unit-variance white noise
xn = sn+vn;     % Sinewave embedded in white noise
plot(n,xn);
save xn.dat xn -ascii ;


http://www.ppmy.cn/ops/16139.html

相关文章

springboot如何返回中文json,保证顺序。LinkedHashMap应用实例

在业务中有时候需要中文json去进行映射到有些UI上,而springboot都是英文字段 //通过id查询消火栓的基本信息和检测值给POIGetMapping("/queryPOIForHydrant")ApiOperationSupport(order 4)ApiOperation(value "查询所需要的消火栓数据渲染给POI&qu…

【力扣 Hot100 | 第五天】4.20(回文链表)

1.回文链表 1.1题目 给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。 示例一: 输入:head [1,2,2,1] 输出:true示例二: 输入…

Java面试题:介绍一下jvm中的内存模型?说明volatile关键字的作用,以及它如何保证可见性和有序性。

JVM(Java Virtual Machine)中的内存模型是一个重要的概念,它主要涉及到JVM如何管理内存以支持Java程序的运行。这个内存模型可以大致划分为以下几个部分: 程序计数器:用于存放下一条指令所在单元的地址,是…

ubuntu20.04开机运行java的sh脚本

用到了 rc.local 1、修改 /usr/lib/systemd/system/rc-local.service 在最下面添加 [Install] WantedBymulti-user.target 2、 系统没有 rc.local,需要手动创建 cd /etc vi rc.local在里面写入 /opt/start.sh chmod x /etc/rc.local # 添加可执行权限 chmod x…

文件不存在创建并写入数据

一、背景 之前遗留了一个问题&#xff0c;就是c没有文件创建文件的代码&#xff0c;这边整理一下&#xff0c;下次可以直接使用 二、实现 #include <iostream> #include <fstream> #include <vector>#define PATH_SEPARATOR_CHAR / #define PATH_SEPARATOR…

MySQL无法远程连接方案解决(示例)

应老表要求写一个MySQL无法远端访问的解决方案&#xff0c;不要问我怎么知 道&#xff0c;因为Oracle厂长是我表弟。。。 按照下面的步骤&#xff0c;一步一步执行一遍即可&#xff1a; -- 第一步&#xff1a;登录好 mysql -u root -p -- 第二步&#xff1a;更改host为全匹…

Qt实现XYModem协议(四)

1 概述 XMODEM协议是一种使用拨号调制解调器的个人计算机通信中广泛使用的异步文件运输协议。这种协议以128字节块的形式传输数据&#xff0c;并且每个块都使用一个校验和过程来进行错误检测。使用循环冗余校验的与XMODEM相应的一种协议称为XMODEM-CRC。还有一种是XMODEM-1K&am…

设计模式之享元模式

一、详细介绍 享元模式是一种结构型设计模式&#xff0c;通过共享对象来有效支持大量细粒度对象的复用&#xff0c;减少内存消耗并提高性能。这种模式通过共享已经存在的相同或相似对象&#xff0c;而不是每次请求都创建新的对象&#xff0c;来减少系统中对象的数量&#xff0c…