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. JavaScript语法规则+JavaScript数据类型

    JavaScript: ECMAScript + BOM +DOM javascript 标识符命名规则: 1.只能是字母.数字.下划线.$ 2.不能以数字开头 3.不能使用关键字和保留字 省略var ...

  2. Vue中vue-i18n结合element-ui实现国际化

    (一)添加依赖模块 在package.json文件中添加vant模块的依赖,如: // package.json { "dependencies": { "element ...

  3. python读取mongodb并提供接口

    # -*- coding: utf-8 -*- import flask, json import time import pymongo client=pymongo.MongoClient(hos ...

  4. redis的一些常见面试题

    1 .在项目中缓存是如何使用的? 2.为啥在项目里要用缓存呢? 用缓存,主要用途,高性能和高并发 高性能: 场景举例:假如有这么一个操作,用户发起请求,操作数据库,查出结果,耗时600ms,然后这个结 ...

  5. layui 弹出层layer中from初始化 ,并在btn中返回from.data

    1.弹出对话框 layer.open() 来初始化弹层 // 监听添加操作 $(".data-add-btn").on("click", function () ...

  6. Magento2 观察者模式 之 插件

    etc/di.xml 定义如下: <type name="Magento\Quote\Model\Quote\Item\ToOrderItem"> <plugin ...

  7. MySQL manager or server PID file could not be found!

    [root@centos var]# service mysqld stop MySQL manager or server PID file could not be found!       [F ...

  8. c++多线程编程——初探

    c++多线程并发可以帮助我们挖掘CPU的性能,在我们的思想中,似乎程序都是顺序执行的.这样的结论是建立在:程序是单线程程序.(比如我们平时写的hello world程序). 但是如果程序是多线程的.那 ...

  9. srpingboot web - 启动(3) 监听器

    接上一篇 一. getRunListeners() 在run() 方法中调用了 getRunListeners(args) 方法, 先看一下这个方法干了什么 private SpringApplica ...

  10. 什么人适合学习Django?

    什么人适合学习Django? 我觉得如果你能满足以下2个条件,你就可以果断地选择Django了. 你喜欢Python, 你对Web开发有热情. 学习Python和Django并不难,最重要的是你是否对 ...