-
Notifications
You must be signed in to change notification settings - Fork 1
/
wavresample.m
75 lines (64 loc) · 2.08 KB
/
wavresample.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function [S_new, varargout] = wavresample(wavfilename, Fs_new, varargin)
%--------------------------------------------------------------------------
% [S_new, t_new, Fs_orig, t_orig, S_orig] =
% wavresample(wavfilename, Fs_new, varargin)
%--------------------------------------------------------------------------
% opto program
%--------------------------------------------------------------------------
%
% Generates stimulus cache
%
%--------------------------------------------------------------------------
% Input Arguments:
% wavfilename path and name of wav file
% Fs_new desired sample rate
%
% Optional:
% Interpolation method:
% 'linear', 'pchip', or 'spline' (see interp1 for details)
% default is 'pchip'
%
% Output Arguments:
% S_new resampled wav data
% t_new time points for resampled wav data
% Fs_orig original sample rate
% t_orig time points for original wav data
% S_orig original wav data
%--------------------------------------------------------------------------
% See Also: resample, interp1, audioread
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% Sharad J. Shanbhag
% sshanbhag@neomed.edu
%--------------------------------------------------------------------------
% Revision History
% 6 September, 2016 (SJS): function created
%--------------------------------------------------------------------------
if ~exist(wavfilename, 'file')
error('%s: wav file %s not found!', mfilename, wavfilename);
end
if nargin < 2
error('%s: need 2 input args, wav file and new sample rate');
end
if ~isempty(varargin)
method = varargin{1};
else
method = 'pchip';
end
[S_orig, Fs_orig] = audioread(wavfilename);
% build time base for orig data
t_orig = (0:(length(S_orig) - 1)) * (1/Fs_orig);
% resample original data
[S_new, t_new] = resample(S_orig, t_orig, Fs_new, method);
if nargout >= 2
varargout{1} = t_new;
end
if nargout >= 3
varargout{2} = Fs_orig;
end
if nargout >= 4
varargout{3} = t_orig;
end
if nargout == 5
varargout{4} = S_orig;
end