电音音效 matlab,数字信号处理Day1自制电子音乐

第一天的课程感觉比較简单,主要介绍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);

db9c08b0abdf6556615a5f812cd4d0e9.png

y

= ks_loop(x, 0.9, 10);

stem(y);

21ee2b28a803fd576a24c618e7207669.png

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

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

134c9c8a0f12fd4bcbe2635bffc3d99d.png

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是频率

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

4678bbafb41b976ecffb086da8148e06.png

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);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值