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. 码云配合git入门命令总结学习

    目录 码云配合git入门命令总结学习 基本设置 基本命令总结学习 准备工作以及基本思路 基本命令 码云搭建仓库步骤 准备前工作 具体操作方法 远程仓库基本命令 标签相关命令 所有命令总结 基本命令总结 ...

  2. Vue(八)---组件(Component)

    组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码. 注册一个全局组件语法格式如下: Vue.component(tagName, optio ...

  3. Wannafly Winter Camp 2020 Day 5B Bitset Master - 时间倒流

    有 \(n\) 个点的树,给定 \(m\) 次操作,每个点对应一个集合,初态下只有自己. 第 \(i\) 次操作给定参数 \(p_i\),意为把 \(p_i\) 这条边的两个点的集合合并,并分别发配回 ...

  4. Linux进程间通信-管道深入理解(转)

    原文地址:https://www.linuxidc.com/Linux/2018-04/151680.htm Linux进程通信系列文章将详细介绍各种通信方式的机制和区别 1.进程间通信 每个进程各自 ...

  5. STL-list 链表

    #include <iostream> #include <list> using namespace std; int main() { // list可以在头部和尾部插入和 ...

  6. UniGUI之UniLabel(31)

    1]改变文本Caption 2]改变颜色字体Font 3]点击访问网址,OnClick事件 1]改变文本Caption unilabel1.Caption:='unilabel1文本'; 2]改变颜色 ...

  7. js里cookie,localStorage的简单用法

    cookie的使用场景---->注册功能(注册成功后,将账号和密码存入cookie里,在登录时显示到输入框里) 注册的js(将数据存入cookie):        escape和unescap ...

  8. 如何在Mac OS X上将PDF转换为Microsoft Word

    Lighten PDF to Word Converter for Mac是一个简单但功能强大的应用程序,可将PDF文件准确,轻松地转换为Microsoft Word.它可以保留原始内容的布局,格式, ...

  9. 字符串hash补充(模数情况下)

    字符串模板,在模数意义下的,比较好用 #include<stdio.h> typedef long long LL; /*[字符串哈希算法] 字符串哈希算法的提出,涉及到如何快速地求两个字 ...

  10. SocketAsyncEvent方式的Server

    1.AsyncUserToken public class AsyncUserToken { /// <summary> /// 客户端IP地址 /// </summary> ...