-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_chirp2.m
198 lines (186 loc) · 6.81 KB
/
dataset_chirp2.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
clear
% signal length
n = 1024;
% kernel length
kn = n/2;
% number of spikes in the signal
k = 5;
% number of observations to make
m = 80;
% frequency chirp
maxf = 20;
minf = .1;
maxt = 2;
%% chirp kernel
t = linspace(0,maxt,kn);
chirp_kernel = chirp(t,minf,maxt,maxf);
chirp_kernel = fade( chirp_kernel, kn/maxt, 400, { @(N)(hann(N)) @(N)(hann(N)) });
% figure(1)
% plot(t,chirp_kernel, 'LineWidth', 1.2)
% set(gcf, 'Position', [500, 500, 600, 120]);
%% random impulse
impulse_rnd = zeros(n, 1);
q = randperm(n);
for i = 1:k
impulse_rnd(q(i)) = 2*rand() - 1;
end
t2 = linspace(0,2*maxt,n);
% figure(2)
% stem(t2,impulse_rnd, 'LineWidth', 1.2)
% ylim([-1, 1])
% set(gcf, 'Position', [500, 500, 600, 120]);
% output signal
output = conv(impulse_rnd, chirp_kernel);
output = output(1:1024); %1024
figure(1)
subplot 311
plot(t,chirp_kernel, 'LineWidth', 1.2)
subplot 312
stem(t2,impulse_rnd, 'LineWidth', 1.2)
ylim([-1, 1])
subplot 313
plot(t2,output, 'LineWidth', 1.2)
ylim([-2, 2])
set(gcf, 'Position', [500, 100, 600, 600]);
function signal = fade( signal, fs, T, window )
% FADE Fade (taper) signal at extremities.
%
% FADE(SIGNAL,FS,T,WINDOW) returns SIGNAL with leading and trailing
% samples faded-in and faded-out, respectively. The signal is sampled
% at FS Hz. The fade-in and fade-out durations are specified in T.
% The window function used for fading is supplied in WINDOW as a
% function handle.
%
% Inputs
% SIGNAL input signal a vector.
%
% FS sampling frequency (Hz).
%
% T fade-in and fade-out durations (ms), as scalar (if same),
% or as a two element vector.
%
% WINDOW function handles for window functions used for
% fading-in and fading-out of the input signal,
% as a single function handle, if same window
% is to be used for fading-in and fading-out,
% or as a two element cell array of window function
% handles, if different window functions are to be used.
%
% Outputs
% SIGNAL input signal with faded leading and trailing samples.
%
% Examples
% clear all; close all; clc; % clear MATLAB environment
%
% fs = 16E3; % signal sampling frequency (Hz)
% duration = 1E3; % signal duration (ms)
% length = round(duration*1E-3*fs); % signal length (samples)
% time = [ 0:length-1 ] / fs; % signal time vector (s)
% fade_durations = [ 350 100 ]; % fade-in and fade-out durations (ms)
%
% % fade-in and fade-out window function handles
% fade_windows = { @(N)(hanning(N).^2) @(N)(chebwin(N,100)) };
%
% original = ones( 1, length );
% faded = fade( original, fs, fade_durations, fade_windows );
%
% % plot generated tones
% hfig = figure( 'Position', [ 10 10 500 300 ], 'PaperPositionMode', 'auto', 'color', 'w');
% plot( time, original, 'r--', 'linewidth', 1.25 ); hold on;
% plot( time, faded, 'b' );
% xlim( [ min(time) max(time) ] );
% ylim( [ min(faded)-0.05*max(faded) 1.05*max(faded) ] );
% xlabel( 'Time (s)', 'FontSize', 7 );
% ylabel( 'Amplitude', 'FontSize', 7 );
% hleg = legend( 'Original', 'Tapered', 4 );
% set( hleg, 'box', 'off', 'Position', get(hleg,'Position')-[0.05 0 0 0] );
% set( gca, 'box', 'off', 'FontSize', 7 );
%
% % print figure to png file
% print( '-dpng', 'fade.png' );
% Author: Kamil Wojcicki, UTD, November 2011.
% check that required inputs have been supplied
if nargin~=4, help(mfilename); return; end;
% check that input is a vector
if min( size(signal) )>1
error( 'Input signal has to be a vector.\n' );
end
% if only one fade was duration specified,
% use it for both fade-in and fade-out durations
if length(T)==1, T = [ T T ]; end;
% if only one window was specified,
% use it for both fade-in and fade-out windows
if length(window)==1, window = { window window }; end;
% get input signal length
L = length( signal );
% determine fade durations (samples)
N = round( T*1E-3*fs );
% determine whether the input signal is in row or column form
if size(signal,1)==1, form='row';
else size(signal,2)==1, form='col';
end
% design fade-in window
fadein = fade_window( N(1), 'fadein', window{1}, form );
% generate fade-out indices
index = 1:N(1);
% apply fade-in window
signal(index) = signal(index) .* fadein;
% design fade-out window
fadeout = fade_window( N(2), 'fadeout', window{2}, form );
% generate fade-out indices
index = L-N(2)+1:L;
% apply fade-out window
signal(index) = signal(index) .* fadeout;
function window = fade_window( N, type, custom, form )
% FADE_WINDOW Design window for signal fading-in or fading-out.
%
% FADE_WINDOW(N,TYPE,CUSTOM) returns WINDOW of length N samples
% for leading or trailing sample tapering of a signal as specified
% by TYPE. Custom window function can be specified.
%
% Inputs
% N fading duration (samples).
%
% TYPE fading direction as string,
% i.e., 'fade-in' or 'fade-out'
%
% CUSTOM function handle for tapering window function to
% be used for fade-in or fade-out window design.
% Note that symmetric tapered window is assumed,
% and that for design purposes window of double
% the length requested will be generated and then
% cut in half to retain the slope-up portion.
%
% FORM spefies as string if the window should be
% in row (i.e., 'row') or column (i.e., 'col') form.
%
% Outputs
% WINDOW window to be used for fading.
% Author: Kamil Wojcicki, UTD, November 2011.
% generate window samples
if false
% some hard-coded examples
% window = triang( 2*N );
% window = chebwin( 2*N, 100 );
window = hanning( 2*N ).^2;
% window = exp( linspace( -4,0,N ) );
else
% use a custom window instead
window = custom( 2*N );
end
% ensure correct vector form
switch lower( form )
case 'col', window = window(:);
case 'row', window = window(:).';
end
% truncate to half length (if 2*N was used)
window = window(1:N);
% scale to unit magnitude
window = window / max( abs(window) );
% time reverse for 'fade-out' option, ignore otherwise
switch lower( type )
case { 'fadeout', 'fade-out' },window = window(end:-1:1);
end
% EOF
end
end