soundtouch implement of changing rate in a way same with resample(SRC).

When rate < 1, it need interpolate sample. and delete samples when rate > 1.

After  interpolation, there may be introduce high frequency. To avoid aliase, we usally apply a low pass filter on interpolated signal.

For the case of deleting samples, some signal may contains high frequecy, the difference between sample may be very sharp. Therefore, we use low pass filter before deleting samples.

When design fir low pass filter, we usaully use "w" (rad/s) as the main parameter instead freqence "f".

The relationship of "w" and "f" as following:

w = 2*pi * f/ fs;

For cutoff frequency, wc = 2*pi * fc / fs = 2 * pi * fc/(2*fn) =  pi * fc / fn, where fc is cutoff frequency, fn is Nywuist frequency.

For fc = = fn, wc = pi;

We use cubic method to interplate sample, the principle of cubic interpolation refer to https://www.paulinternet.nl/?page=bicubic

%calc low pass filter coefficient. The low pass filter based on sinc function with hamming window.

function coeff = calCoeffs(cutoffFreq, len)

coeff = zeros(len ,1);

wc = 2 * pi * cutoffFreq;

tempCoeff = 2 * pi / len;

sum = 0;

for i = 0 : 1 : len -1

  cntTemp = (i - len/2);

  temp = cntTemp  * wc;

  % sinc function

  if temp ~=0

    h = sin(temp) / temp;

  else

    h = 1;

  end

  %hamming window

  w = 0.54 + 0.46 * cos(tempCoeff * cntTemp);

  coeff(i+1) = w * h;

  sum = sum + coeff(i+1);

end

coeff = coeff / sum;

end

function output = firfilter(input, coeff)

inputLen = length(input(:, 1));

filterLen = length(coeff(:, 1));

output = zeros(inputLen ,1 );

outputLen = inputLen - filterLen;

for i = 1: 1: outputLen

  inpos = i;

  sum = 0;

  for j = 1:1:filterLen

    sum = sum + input(inpos ,1) * coeff(j, 1);

    inpos = inpos + 1;

  end

  output(i, 1) = sum;

end

end

function output = cubicInterpolation(input, rate)

inputLen = length(input(:,1));

outputLen = floor(inputLen / rate);

output = zeros(outputLen ,1);

inputIdx = 1;

fract = 0;

outputIdx = 1;

while inputIdx < inputLen - 4

  x1 = fract;

  x2 = x1 * x1;

  x3 = x1 * x2;

  p0 = input(inputIdx , 1);

  p1 = input(inputIdx + 1 , 1);

    p2 = input(inputIdx + 2, 1);

  p3 = input(inputIdx + 3, 1);

  output(outputIdx ,1) = (-0.5*p0 + 1.5*p1 -1.5 *p2 + 0.5*p3) * x3 +(p0 - 2.5*p1 + 2*p2 -0.5*p3) *x2 + (-0.5*p0 + 0.5*p2) * x1 + p1;

  outputIdx = outputIdx + 1;

  fract = fract + rate;

  whole = floor(fract);

  fract = fract - whole;

  inputIdx = inputIdx + whole;

end

end

function output = linearInterpolation(input, rate)

inputLen = length(input(:,1));

outputLen = floor(inputLen / rate);

output = zeros(outputLen ,1);

inputIdx = 1;

fract = 0;

outputIdx = 1;

while inputIdx < inputLen - 4

  p0 = input(inputIdx , 1);

  p1 = input(inputIdx + 1 , 1);

  output(outputIdx ,1) = (1-fract) * po + fract * p1;

  outputIdx = outputIdx + 1;

  fract = fract + rate;

  whole = floor(fract);

  fract = fract - whole;

  inputIdx = inputIdx + whole;

end

end

function output = changeRate(input, rate, interpMethod)

inputLen = length(input(:, 1));

outputLen = floor(inputLen / rate);

output = zeros(outputLen, 1);

if rate > 1

  cutoffFreq = 0.5 / rate;

else

  cutoffFreq = 0.5 * rate;

end

filterLen = 64;

coeff = calCoeffs(cutoffFreq, filterLen);

if rate < 1

  %slow down, need interpolation first;

  if strcmp(interMethod, 'cubic')

    output = cubicInterpolation(input, rate);

  else

    output = linearInterpolation(input, rate);

  end

  output = firfilter(output, coeff);

else

  %fast, need filter out the high freqency, then delete samples

  output = firfilter(input, coeff);

  if strcmp(interMethod, 'cubic')

    output = cubicInterpolation(output, rate);

  else

    output = linearInterpolation(output, rate);

  end

end

end

main.m:

clc;

clear all;

[input fs] = wavread('input.wav');

%if do SRC, rate = inputfs / outputfs;

rate = 0.5;

output = changeRate(input, rate, 'cubic');

wavwrite(output, fs, 'output.wav);

soundtouch change rate matlab implementation的更多相关文章

  1. soundtouch change pitch matlab implementation

    function output = changePitch(input, pitchInSemitones) % one octave is 12 semitones octave = pitchIn ...

  2. soundtouch 变速算法matlab实现

    soundtouch变速主要采用WSOLA算法来进行变速. http://www.surina.net/soundtouch/ https://blog.csdn.net/suhetao/articl ...

  3. Viola–Jones object detection framework--Rapid Object Detection using a Boosted Cascade of Simple Features中文翻译 及 matlab实现(见文末链接)

    ACCEPTED CONFERENCE ON COMPUTER VISION AND PATTERN RECOGNITION 2001 Rapid Object Detection using a B ...

  4. MATLAB绘图

    matlab绘制散点图 clc,clear x=[11.9,11.5,14.5,15.2,15.9,16.3,14.6,12.9,15.8,14.1]; y=[196.84,196.84,197.14 ...

  5. VS Code中Matlab插件安装设置

    Install the extension in VS Code Open the command palette using Ctrl+Shift+P Type ext install Matlab ...

  6. Frequency-tuned Salient Region Detection MATLAB代码出错修改方法

    论文:Frequency-tuned Salient Region Detection.CVPR.2009 MATLAB代码运行出错如下: Error using makecform>parse ...

  7. `GLIBCXX_3.4.15' not found when using mex file in matlab (linux)

    from: http://www.360doc.com/content/14/0314/16/175261_360565922.shtml Invalid MEX-file '*/*/*.mexa64 ...

  8. {ICIP2014}{收录论文列表}

    This article come from HEREARS-L1: Learning Tuesday 10:30–12:30; Oral Session; Room: Leonard de Vinc ...

  9. 计算机视觉code与软件

    Research Code A rational methodology for lossy compression - REWIC is a software-based implementatio ...

随机推荐

  1. 令人抓狂的redis和rediscluster Python驱动包的安装

    本文环境:centos 7,Python3编译安装成功,包括pip3,然后需要安装redis相关的Python3驱动包,本的redis指redis包而非redis数据库,rediscluster类似. ...

  2. 【剑指Offer】58:二叉树的下一个结点

    题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 题解一:递归 //既然给了二叉树的某个结点,且二叉树存 ...

  3. MariaDB(MySQL)创建、删除、选择及数据类型使用详解

    一.MariaDB简介(MySQL简介略过) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行 ...

  4. 使用 Express 脚手架

    安装: npm install -g express-generator 创建项目: express myapp 安装依赖 install dependencies: > npm install ...

  5. PHP0007:PHP基础-字符串

    php设置编码 用gbk编码识别utf8字符

  6. JAVA->查询并显示输入根目录下全部的文件所在目录路径

    public static boolean qf(File f,boolean a){      boolean b=false;   if(a==true){      File[] fl=f.li ...

  7. C#的隐式类型、匿名类型、自动属性、初始化器

    1.隐式类型 1)源起 在隐式类型出现之前,我们声明一个变量时,需要为它指定相应的类型,甚至在foreach一个集合的时候,也要为遍历的集合元素,指定变量的类型,隐式类型出现后,程序员就不用再做这个工 ...

  8. cocos studio 中交互性

    cocos studio 属性->常规->交互性,勾选之后相当于代码中的setTouchEnabled(true)

  9. gulp常用插件之gulp-htmlmin使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-htmlmin这是一款HTML文件压缩插件. 更多使用文档请点击访问gulp-htmlmin工具官网. 安装 一键安装不多解释 npm ...

  10. gulp常用插件之autoprefixer使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 autoprefixer这是一款自动管理浏览器前缀的插件,它可以解析CSS文件并且添加浏览器前缀到CSS内容里. 更多使用文档请点击访问autop ...