soundtouch change rate matlab implementation

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

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

原文地址:https://www.cnblogs.com/fellow1988/p/10004541.html

时间: 2024-11-08 00:52:07

soundtouch change rate matlab implementation的相关文章

soundtouch change pitch matlab implementation

function output = changePitch(input, pitchInSemitones) % one octave is 12 semitones octave = pitchInSemitones / 12; %0.69314718056 is In2. go up one octave corresponds to twice the freqency; pitchChange = exp(0.69314718056  * octave); rate = 1 * pitc

《数字图像处理原理与实践(MATLAB版)》一书之代码Part9

本文系<数字图像处理原理与实践(MATLAB版)>一书之代码系列的Part9,辑录该书第431至第438页之代码,供有需要读者下载研究使用.至此全书代码发布已经接近尾声,希望这些源码能够对有需要的读者有所帮助.代码执行结果请参见原书配图,建议下载代码前阅读下文: 关于<数字图像处理原理与实践(MATLAB版)>一书代码发布的说明 http://blog.csdn.net/baimafujinji/article/details/40987807 首先给出的是原书P438所列之程序源

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

论文:Frequency-tuned Salient Region Detection.CVPR.2009 MATLAB代码出错: Error using makecform>parseWPInput (line 389)Expected input number 2, PROPERTYNAME, to match one of these strings: AdaptedWhitePoint The input, ''whitepoint'', did not match any of the

{ICIP2014}{收录论文列表}

This article come from HEREARS-L1: Learning Tuesday 10:30–12:30; Oral Session; Room: Leonard de Vinci 10:30  ARS-L1.1—GROUP STRUCTURED DIRTY DICTIONARY LEARNING FOR CLASSIFICATION Yuanming Suo, Minh Dao, Trac Tran, Johns Hopkins University, USA; Hojj

计算机视觉code与软件

Research Code A rational methodology for lossy compression - REWIC is a software-based implementation of a a rational system for progressive transmission which, in absence of a priori knowledge about regions of interest, choose at any truncation time

paper 15 :整理的CV代码合集

这篇blog,原来是西弗吉利亚大学的Li xin整理的,CV代码相当的全,不知道要经过多长时间的积累才会有这么丰富的资源,在此谢谢LI Xin .我现在分享给大家,希望可以共同进步!还有,我需要说一下,不管你的理论有多么漂亮,不管你有多聪明,如果没有实验来证明,那么都是错误的.  OK~本博文未经允许,禁止转载哦!  By  wei shen Reproducible Research in Computational Science “It doesn't matter how beautif

International Macroeconomics

International MacroeconomicsContents1 Programming I: Introduction to Matlab 12 Programming II: Use of Dynare to Simulate Dynamic Models 82.1 Baseline Theoretical Example . . . . . . . . . . . . . . . . . . . . . . . . . . 82.2 Steady Operationalizati

day6:vcp考试

Q101. Refer to the Exhibit.Which tab shows the Hardware Acceleration support status?A. DevicesB. PropertiesC. PathsD. Advanced Options Q102. Which minor badge items make up the Efficiency badge score for an ESXi host in vCenter Operations Manager?A.

Awesome Machine Learning

Awesome Machine Learning  A curated list of awesome machine learning frameworks, libraries and software (by language). Inspired by awesome-php. If you want to contribute to this list (please do), send me a pull request or contact me @josephmisiti Als