-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspikelet.m
212 lines (127 loc) · 3.95 KB
/
spikelet.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
199
200
201
202
203
204
205
206
207
208
209
210
211
%% Matching Wavelet - Spikelet Algorithm - Generate Filter Coefficients
% Algorithm - Prof. Dr. Rodrigo Capobianco Guido, University of Sao Paulo,Brazil
% Matlab Program - Arun Kumar A , Santhom Computing Facility
% 13-05-10
% Filter Support - 4
%% Initializations
clc;
clear all % Clear the Workspace
%% Load Data File
% Data file should contain a data vector of any format csv or text
file_name=input('Enter filename with extension : ','s');
data=load(file_name); % Data loaded into variable "data"
% Choice for saving coefficients and wavelet
choice=input('Do you want to save the generated coefficients and wavelets (Y/N) : ' ,'s');
if (choice=='Y')
hname=input('Enter filename to store coefficients (with extension) : ','s');
wtname=input('Enter filename to store wavelet (with extension) : ','s');
end
%% Declarations and Initial Variables
filt_sup=4; % Filter Support
l=length(data); % Length of data vector
%% Set filter coefficients for Daubechies ( Now set for testing ) - Stage A1-A3
% h0
a1 = 1;
a2 = 0;
a3 = 1;
% h1
b1 = -1;
b2 = -1;
b3 = 1;
% h2
c1 = 1;
c2 = 2;
c3 = 1;
% h3
d1 = -1;
d2 = -3;
d3 = 1;
cof1=[d1,c1,b1,a1;d2,c2,b1,a2;d3,c3,b3,a3];
%% Stage B1
norm_fac=abs(min(data)); % Normalisation factor
norm_vec= data/norm_fac; % Normalisation
f=norm_vec;
%% Stages B2 & B3
cof2=zeros(1,filt_sup);
for j=0:filt_sup-1
for k=0:filt_sup-1
for i=0:round(l/2)-1
in1=rem((i*2+j),(l-1));
in2=rem((i*2+k),(l-1));
r(i+1) = f(in1+1)*f(in2+1);
end
cof2(k+1,j+1)=sum(r);
end
end
%% Stage C1
for i=0:filt_sup-1
cof2(:,i+1)=((-1)^i*cof2(:,i+1));
end
B=vertcat(cof2,cof1);
C=[0;0;0;0;0;0;2];
%% Stage C2
A=horzcat(cof2,cof1');
%% Stage C3
% Solve for Coefficients
h=flipud(B\C);
g=flipud(h);
for j=0:filt_sup-1
g(j+1)=((-1)^j)*g(j+1);
end
disp('Low Pass Filter Coefficients')
disp(' ')
for i=1:filt_sup
disp(sprintf(' h%i = %f',i-1,h(i)))
end
disp('-------------------')
disp('High Pas Filter Coefficients')
disp(' ')
for i=1:filt_sup
disp(sprintf(' g%i = %f',i-1,g(i)))
end
%% Generate Scaling Function
phi(1)=0;
phi(7)=0;
A1=[h(2)-1 h(1);h(4) h(3)-1; 1 1];
B2=[0;0;1];
C=A1\B2;
phi(3)=C(1);
phi(5)=C(2);
phi(2)=h(1)*phi(3);
phi(4)=(h(2)*phi(5))+(h(3)*phi(3));
phi(6)=h(4)*phi(5);
%-------------------------------------------------------------|
% In matlab, indexing is different that from other languages. |
% In the actual paper the index values differ from what is |
% written in the program. The actual indexes are as below. |
% The first phi is the notation in the code and the 2nd of |
% the paper. |
% phi(1) => phi(0), phi(2) => phi(1/2), phi(3) => phi(1) |
% phi(4) => phi(3/2), phi(5) => phi(2), phi(6) => phi(5/2) |
% phi(7) => phi(3) . |
% ------------------------------------------------------------|
%% Wavelet Function
psi(1)=g(1)*phi(1);
psi(3)=(g(1)*phi(5))+(g(2)*phi(3))+(g(3)*phi(1));
psi(5)=(g(2)*phi(7))+(g(3)*phi(5))+(g(4)*phi(3));
psi(7)=g(4)*phi(7);
psi(2)=g(1)*phi(3);
psi(4)=(g(2)*phi(5))+(g(3)*phi(3));
psi(6)=g(4)*phi(5);
%% Plot all important stuff
subplot(2,2,1),plot(data),title('Original data');
subplot(2,2,2),plot(f),title('Normalised data');
subplot(2,2,3),plot(phi),title('Scaling Function - phi');
subplot(2,2,4),plot(psi),title('Wavelet - psi');
%% Write Coefficients and Wavelet to file
if ( choice=='Y')
dlmwrite(hname,h,'delimiter',',');
disp(sprintf('Created file %s',hname));
dlmwrite(wtname,psi,'delimiter',',');
disp(sprintf('Created file %s',wtname));
disp('All task Completed Successfully !');
else
disp('All task Completed Successfully !');
end
%% End