第一天的课程感觉比較简单,主要介绍Karplus-Strong Algorithm

给出方程
y[n]=αy[n−M]+x[n],

x[n]是输入,M是延迟,α是衰弱系数

我们要衰减D次,总的採样数就是D*M

以下是最直接的实现

关于x
= x(:).';的语法是这种,这是一个转置,可是是非共轭转置,假设是x',那么1+i就成了1-i

function y = ks_loop(x, alpha, D)

% Length of the output signal must be larger than the length of the input signal,
% that is, D must be larger than 1
if D < 1
error('Duration D must be greater than 1');
end % Make sure the input is a row-vector
x = x(:).'; % Number of input samples
M = length(x); % Number of output samples
size_y = D * M; % Initialize with random input x
y = zeros(1, size_y);
y(1:M) = x; for index = (M+1):size_y
y(index) = alpha * y(index - M);
end y = y(:); return

以下来測试一下

x = randn(100, 1);

stem(x);

y
= ks_loop(x, 0.9, 10);

stem(y);

事实上,你已经完毕了KS算法

要知道,在matlab和octave这种软件中,矩阵运算比单个运算速度要快非常多,于是就有了优化的版本号

function y = ks(x, alpha, D)

% Length of the output signal must be larger than the length of the input signal,
% that is, D must be larger than 1
if D < 1
error('Duration D must be greater than 1.');
end % Make sure the input is a row-vector
x = x(:).'; % Number of input samples
M = length(x); % number of output samples
size_y = D * M; % Create a vector of the powers of alpha, [alpha^0 alpha^1 ....]
size_alphaVector = D;
alphaVector = (alpha*ones(size_alphaVector,1)).^((0:(size_alphaVector-1))'); % Create a matrix with M columns, each being the vector of the powers of alpha
alphaMatrix = repmat(alphaVector, 1, M); % Create a matrix with D rows filled by the input signal x
xMatrix = repmat(x, D, 1); % Multipliy the two, and take the transpose so we can read it out
% column-by-column
yMatrix = (alphaMatrix .* xMatrix).'; % Read out the output column by columnn
y = yMatrix(:); return

在matlab中,你能够用soundsc(y, FS)来播放音乐

y是我们的採样数据,FS是频率

以下这个样例能够播放opening
chord of Hard day's night
开头的音乐,太奇妙了

由于牵扯到音乐的相关知识,一些參数就不大懂,仅仅画出了最后的採样图看看

clear all
close all
clc % Parameters:
%
% - Fs : sampling frequency
% - F0 : frequency of the notes forming chord
% - gain : gains of individual notes in the chord
% - duration : duration of the chord in second
% - alpha : attenuation in KS algorithm Fs = 48000; % D2, D3, F3, G3, F4, A4, C5, G5
F0 = 440*[2^-(31/12); 2^-(19/12); 2^-(16/12); 2^(-14/12); 2^-(4/12); 1; 2^(3/12); 2^(10/12)];
gain = [1.2 3.0 1.0 2.2 1.0 1.0 1.0 3.5];
duration = 4;
alpha = 0.9785; % Number of samples in the chord
nbsample_chord = Fs*duration; % This is used to correct alpha later, so that all the notes decay together
% (with the same decay rate)
first_duration = ceil(nbsample_chord / round(Fs/F0(1))); % Initialization
chord = zeros(nbsample_chord, 1); for i = 1:length(F0) % Get M and duration parameter
current_M = round(Fs/F0(i));
current_duration = ceil(nbsample_chord/current_M); % Correct current alpha so that all the notes decay together (with the
% same decay rate)
current_alpha = alpha^(first_duration/current_duration); % Let Paul's high D on the bass ring a bit longer
if i == 2
current_alpha = current_alpha^.8;
end % Generate input and output of KS algorithm
x = rand(current_M, 1);
y = ks(x, current_alpha, current_duration);
y = y(1:nbsample_chord); % Construct the chord by adding the generated note (with the
% appropriate gain)
chord = chord + gain(i) * y;
end % Play output
soundsc(chord, Fs);

数字信号处理Day1自制电子音乐的更多相关文章

  1. 音乐制作:用FL Studio做电子音乐

    电音制作,自然少不了适合做电音的软件,市面上可以进行电音制作的软件不少,可是如果在这些软件中只能选择一款的话,想必多数人会把票投给FL Studio,毕竟高效率是永远不变的真理,今天就让我们来看看如何 ...

  2. 数字信号处理--FFT与蝶形算法

    在数字信号处理中常常需要用到离散傅立叶变换(DFT),以获取信号的频域特征.尽管传统的DFT算法能够获取信号频域特征,但是算法计算量大,耗时长,不利于计算机实时对信号进行处理.因此至DFT被发现以来, ...

  3. 数字信号处理与音频处理(使用Audition)

    前一阵子由于考博学习须要,看了<数字信号处理>,之前一直不清除这门课的理论在哪里应用比較广泛. 这次正巧用Audition处理了一段音频,猛然发现<数字信号处理>这门课还是很实 ...

  4. 数字信号处理MATLAB简单序列

    数字信号处理应用的几个基本序列: 1 单位样本序列 function mainImseq() clc clear disp('生成抽样序列'); y=imseq(,,); %调用样本函数,此时序列下标 ...

  5. FPGA与数字信号处理

    过去十几年,通信与多媒体技术的快速发展极大地扩展了数字信号处理(DSP)的应用范围.眼下正在发生的是,以更高的速度和更低的成本实现越来越复杂的算法,这是针对高级信息服更高带宽以及增强的多媒体处理能力等 ...

  6. 数字信号处理专题(3)——FFT运算初探

    一.前言 FFT运算是目前最常用的信号频谱分析算法.在本科学习数字信号处理这门课时一直在想:学这些东西有啥用?公式推来推去的,有实用价值么?到了研究生后期才知道,广义上的数字信号处理无处不在:手机等各 ...

  7. 数字信号处理专题(1)——DDS函数发生器环路Demo

    一.前言 会FPGA硬件描述语言.设计思想和接口协议,掌握些基本的算法是非常重要的,因此开设本专题探讨些基于AD DA数字信号处理系统的一些简单算法,在数字通信 信号分析与检测等领域都会或多或少有应用 ...

  8. 现代数字信号处理——AR模型

    1. AR模型概念观       AR模型是一种线性预测,即已知N个数据,可由模型推出第N点前面或后面的数据(设推出P点),所以其本质类似于插值,其目的都是为了增加有效数据,只是AR模型是由N点递推, ...

  9. 如何使用Matlab做数字信号处理的仿真1

    例如 第三版数字信号处理P51 -1.14习题时域离散信号的相关性研究x(n)=Asin(ωn)+u(n),其中ω=π/16,u(n)是白噪声,现要求 ⑴.产生均值为0,功率P=0.1的均匀分布白噪声 ...

随机推荐

  1. Hibernate之总结

    以前做.net,最近做java项目,负责服务端的开发,直接用的jdbc,线程安全问题.缓存同步问题以及连接池什么的,都是手动写,不但麻烦而且容易出错.项目结束,赶快抽时间学了下hibernate,每天 ...

  2. log实例

    配置详解见2014.10月篇 log4j的pom.xml <dependency> <groupId>log4j</groupId> <artifactId& ...

  3. WPF:构建应用程序

    原文 http://www.cnblogs.com/free722/archive/2011/11/13/2247455.html WPF相关的项目内容包含在App.Xaml和Window1.xaml ...

  4. 排序-java

    今天座右铭----每天的学习会让我们不断地进步! 往往面试中都会让我们用一种排序方法做一道排序题,下面我就罗列出快速排序.冒泡排序.插入排序.选择排序的java代码! 1.快速排序 public cl ...

  5. 梯度下降算法的一点认识(Ng第一课)

    昨天开始看Ng教授的机器学习课,发现果然是不错的课程,一口气看到第二课. 第一课 没有什么新知识,就是机器学习的概况吧. 第二课 出现了一些听不太懂的概念.其实这堂课主要就讲了一个算法,梯度下降算法. ...

  6. java 集合类说明与区别

    集合类说明及区别Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap ...

  7. 面向对象程序设计-C++_课时19const_课时20不可修改的

    error C2131: 表达式的计算结果不是常数 #include <iostream> using namespace std; void main() { ; int finalGr ...

  8. lighttpd配置虚拟主机/php等WEB环境

    lighttpd(1.4.37)配置如下 server.document-root = "/var/www/lighttpd/" server.port = 8888 server ...

  9. 一步一步学android之布局管理器——RelativeLayout

    今天开始学习RelativeLayout(相对布局),相对布局在平时布局的时候用的较多,因为Android适配方面的原因.相对布局可以控制组件摆放的位置(放在任一组件的上下左右等位置),下面来看看类的 ...

  10. Linux系统CentOS6.2版本号下安装JDK7具体过程

    前言:        java 是一种能够撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE( ...