-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhysampling.m
110 lines (102 loc) · 3.27 KB
/
hysampling.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
function [xs,ys]=hysampling(x,y,nval,idlog,option)
%HYSAMPLING - Sample a signal at regular intervals.
%
% Syntax: [xs,ys] = hysampling( x, y, nval [,idlog [,option]] )
%
% x,y = vectors containing the input signal
% nval = number of values of the output signal
% xs,ys = sampled signal
%
% OPTIONAL ARGUMENT
%
% idlog : allows to select the type of spacing of the samples
%
% idlog = 'linear' = Default value = linear scale
% idlog = 'log' = logarithmic scale
%
% option : allows to define if the points must be interpolated
%
% option = 'sample' = Default value
% = only points from the data set are taken
% option = 'interp' = creates points by interpolation
%
% Description:
% The hytool function hysampling samples a data set x,y at regular
% intervals and displays the sampled data set as xs,ys. The input signal
% is interpolated at a given number of points (nval) that are regularly
% spaced either in linear (idlog=’linear’) or in logartihmic (idlog=’log’)
% scale. By option 'sample' (Default value) the data set is simply
% sampled, i.e. points from the data set are taken, sampled points are
% created by interpolation by the option = 'interp' in contrast.
%
% Example:
% [ts,hs] = hysampling(t,s,10)
% [ts,hs] = hysampling(t,s,30,'log')
% [ts,hs] = hysampling(t,s,10,'linear','interp')
% [ts,qs] = hysampling(tf,qf,30,'log','interp')
%
% See also: hyfilter, hyplot, hyclean, hyselect
xs=NaN;
ys=NaN;
if(nargin<3) % Is there at least two input arguments ?
disp(' ');
disp(' ERROR: The function hysampling needs at list three input argument.')
disp(' ');
return
end
if(nval<3) % Is there at least two input arguments ?
disp(' ');
disp(' ERROR: nval must be greater than 3.')
disp(' ');
return
end
if(nval>size(x,1)) % Is there at least two input arguments ?
disp(' ');
disp(' WARNING: nval is larger than the number of data points.')
disp(' ');
end
if(nargin==3) % If only three arguments.
idlog='linear';
option='sample';
end
if(nargin==4) % If only three arguments.
option='sample';
end
if( strcmp(idlog,'log') )
j=find(x>0); % This is to avoid calculating the log of zero
xs=logspace(log10(x(j(1))),log10(x(end)), nval);
elseif( strcmp(idlog,'linear') )
xs=linspace(x(1),x(end),nval);
else
disp(' ');
disp(' SYNTAX ERROR: hysampling: the 4th parameter (idlog) is incorrect.')
disp(' ');
return
end
if( strcmp(option,'sample') )
for i=1:nval
dist=sqrt((x-xs(i)).^2);
mn=min(dist);
j=find(dist==mn);
xs(i)=x(j);
ys(i)=y(j);
end
% When there are not enough data point, the sampling technique
% creates duplicates. They have to be removed.
[xsi,i]=unique(xs);
ys=ys(i);
if(length(xsi)~=length(xs))
disp(' WARNING: hysampling - the number of selected values is smaller than requested');
disp(' because there is not enough data points to sample regularly the signal.');
end
xs=xsi;
elseif( strcmp(option,'interp') )
ys=interp1(x,y,xs);
else
disp(' ');
disp(' SYNTAX ERROR: hysampling: the 5th parameter (option) is incorrect.')
disp(' ');
return
end
xs=xs'; %transpose xs
ys=ys';