开发者

How do i play back a sampled audio file at the same speed as the original?

开发者 https://www.devze.com 2023-02-01 18:54 出处:网络
Question is as stated in the title. After i decimate an audio signal that take every nth point out it in turns speeds up the audio clip at a factor of n. I want the decimated and original clips to ha

Question is as stated in the title.

After i decimate an audio signal that take every nth point out it in turns speeds up the audio clip at a factor of n. I want the decimated and original clips to have the same length in time.

Heres my code, analyzing and decimated piano .wav

[piano,fs]=wavread('piano.wav'); % loads piano
play=piano(:,1); % Renames the file as "play"

t = linspace(0,time,length(play));          % Time vector
x = play;
y = decimate(x,2);

stem(x(1:30)), axis([0 30 -2 2])   % Original signal
title('Original Signal')
figure
stem(y(1:30))                        % Decimated signal
title('Decimated Signal')

%changes the sampling rate

fs1 = fs/2;
fs2 = fs/3;
fs3 = fs/4;
fs4 = fs*2;
fs5 = fs*3;
fs6 = fs*4;

wavwrite(y,fs,'PianoDecimation');

possible solutions: Double each of the remaining points开发者_开发知识库 since the new decimated clip is 2x shorter then the original.

I just want to be able to have a side by side comparison of the 2 clips.

here is the audio file: http://www.4shared.com/audio/11xvNmkd/piano.html


Although @sage's answer has a lot of good information, I think the answer to the question is as simple as changing your last line to:

 wavplay(y,fs/2,'PianoDecimation')

You have removed half the samples in the file, so in order to play it back over the same time period as the original, you need to set the playback frequency to half as many samples per second.


Are you using wavplay, audioplayer, or something else to play the decimated signals? Are you explicitly specifying the sample frequencies?

The functions take the sample frequency as one of the parameters (the second parameter). You are decreasing the sample frequency as you decimate, so you need to update that parameter accordingly.

Also, when you are plotting the data, you should:

  1. plot N times as many points on the original data (when decimating by N)
  2. provide a corresponding x axis input - I recommend t = (1/Fs:1/Fs:maxT) where maxT is the maximum time you want to plot, which will address #1 if you use the updated Fs, which will result in larger time steps (and make sure to transpose t if it does not match your signal)

I have added an example that plays chirp and decimated chirp (this chirp is part of the standard MATLAB install). I amplified the decimated version. The tic and toc show that the elapsed time is equivalent (within variations in processor loading, etc.) - note that this also works for decim = 3, etc:

load chirp

inWav = y;
inFs = Fs;

decim = 2;

outWav = decimate(inWav,decim);
outFs = inFs/decim;

tic, wavplay(inWav,inFs),toc
pause(0.2)
tic,wavplay(outWav*decim^2,outFs),toc

The function 'decimate' really messes up the chirp sound (the sample rate of which is not very high frequency to begin with), but perhaps you are trying to show something like this...

0

精彩评论

暂无评论...
验证码 换一张
取 消