-
Notifications
You must be signed in to change notification settings - Fork 4
/
fade.m
70 lines (62 loc) · 1.86 KB
/
fade.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
function [data]=fade(data, fs, fadein, fadeout, winfunc, windur)
%% DESCRIPTION:
%
% Function to fade sounds in/out by applying a window over a specified
% time range.
%
% Uses MATLAB's "window" function to create the fade in/fade out ramps.
%
% INPUT:
%
% data: times series to fade. Must be a double/single array. Other data
% formats can be supported in the future, provided SIN_loaddata
% can load them easily.
%
% fs: double, sampling rate (Hz) (no default)
%
% fadein: bool, fade signal in (default = false)
%
% fadeout: bool, fade times series out (default = false)
%
% winfunc: function handle for windowing function (default @hann). See
% 'help window' for more information.
%
% windur: window duration in sec (no default)
%
% OUTPUT:
%
% data: faded times series
%
% Development:
%
% Christopher W. Bishop
% University of Washington
% 6/14
%% SET DEFAULTS
if ~exist('fadein', 'var') || isempty(fadein), fadein=false; end
if ~exist('fadeout', 'var') || isempty(fadein), fadeout=false; end
if ~exist('winfunc', 'var') || isempty(winfunc), winfunc=@hann; end
%% LOAD DATA
t.dtype = 1; t.fs=fs;
[data, ~] = SIN_loaddata(data, t);
%% CONVERT TIME TO SAMPLES
% - Need to convert time to samples to create window below
winsamps = round(windur * fs);
%% GET WINDOW
% - Make a window 2x the size requested. This will be cut in half below.
win=window(winfunc, 2*winsamps); % Create onset/offset ramp
% Match number of data_channels
% data_channels are ramped and mixed to match the number of
% physical_channels below.
win=win*ones(1, size(data,2));
% Create fade in ramp
ramp_on=win(1:ceil(length(win)/2),:);
ramp_on=[ramp_on; ones(size(data,1) - size(ramp_on,1), size(ramp_on,2))];
%% APPLY FADE IN
if fadein
data = data.*ramp_on;
end
%% APPLY FADE OUT
if fadeout
data=data.*flipud(ramp_on);
end %