-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwav2sig.m
145 lines (126 loc) · 4.41 KB
/
wav2sig.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function [sig,fs] = wav2sig(fnames, varargin)
% This function reads in wave files and stores all the information into a
% single matrix with equal number of rows, with each column representing
% the different wave files.
%
% sig = wav2sig(fnames)
% sig = wav2sig(fnames,fs)
% sig = wav2sig(fnames,tInt)
% sig = wav2sig(fnames,fs,tInt)
%
% Inputs:
% 1) fnames - name of the wave files in a cell array.
% Format:
% a) for 1 file: {'filename1.wav'}
% b) multiple files: {'filename1.wav';'filename2.wav'}
% NOTE:
% Each wave file should only have one channel. If more than one
% channels are present, only the first channel will be used.
% 2) varargin (optional):
% a) fs - resample the wave file to this frequency
% b) tInt - 1x2 vector to specify time interval (in seconds) to
% trim down to
%
% Output:
% sig - matrix with the following properties:
% a) Number of rows is determined by the number of rows in the
% longest wave file
% i) shorter wave files will be padded with 0's
% b) Number of columns is determined by the number of input wave
% files
%
% Written by Satoru Tagawa (staga2@uky.edu) 6/12/08
% Parameter check ********************************************************
% The function must have at least 1 parameter, and at most 3 parameters
if nargin == 0
error('wavToSigMat must have at least 1 parameter');
end
if nargin > 3
error('There can only be a maximum of 3 parameters');
end
% If more than one parameter, check to see what the parameters are
if nargin > 1
[numR1,numC1] = size(varargin{1});
if numR1 ~= 1 % Argument cannot have more than 1 row
error('2nd parameter must have the dimension 1x1 or 1x2');
end
if length(varargin) == 1 % 2 parameters
if numC1 == 1
fs = varargin{1};
elseif numC1 == 2
tInt = varargin{1};
else
error('2nd parameter must have the dimension 1x1 or 1x2');
end
else % 3 parameters
if numC1 == 1
fs = varargin{1};
else
error('fs must have the dimension 1x1');
end
[numR2,numC2] = size(varargin{2});
if numR2 ~= 1
error('tInt must have the dimension 1x1 or 1x2');
end
if numC2 == 2
tInt = varargin{2};
else
error('tInt must have the dimension 1x2');
end
end
end
%*************************************************************************
% Read in each wave files and place in cell array "y"
for fno=1:length(fnames)
% Read the wave file, determine its sample frequency
[y{fno},nfs(fno)]=wavread(fnames{fno});
% If more than one channel present, eliminate all but the first channel
[nR,nChanOrig] = size(y{fno});
if nChanOrig ~= 1
y{fno} = y{fno}(:,1);
end
end
% If fs is not given, down sample to the signal with the lowest fs
if nargin == 1 || nargin == 2 && numC1 == 2
fs = min(nfs);
end
% Resample the wave files
for fno=1:length(fnames)
yf{fno} = resample(y{fno},fs,nfs(fno));
% Find length of the resampled signal
siglen(fno) = length(yf{fno});
end
maxSigLen = max(siglen);
% Conform the number of rows to that of the longest signal
for fno=1:length(fnames)
if siglen(fno) == maxSigLen
sig(:,fno) = yf{fno};
elseif siglen(fno) < maxSigLen
sig(:,fno) = [yf{fno}; zeros(maxSigLen-siglen(fno),1)];
else
error(['You can''t have a longer signal than the longest', ...
'signal...']);
end
end
% if tInt is passed in, trim down or zero pad 'sig'
if nargin==3 || nargin==2 && numC1 == 2
if tInt(1) > tInt(2)
error('2nd column in tInt must be greater than the 1st column');
elseif tInt(1) < 0 || tInt(2) < 0
error('tInt cannot contain negative values');
end
begin_index = fs*tInt(1)+1;
end_index = fs*tInt(2);
% Warning if begin_index is greater than signal length
if begin_index > length(sig)
warning('Did you mean to create a silent signal?');
end
% zero pad if end_index is greater than the signal length
if end_index > length(sig)
sig = [sig; zeros(end_index-length(sig),length(fnames))];
end
sig = sig(begin_index:end_index,:);
end
% Write out to a wav file
% For debugging purpose
% wavwrite(sig,fs,'out.wav');