-
Notifications
You must be signed in to change notification settings - Fork 5
/
WithPreFiltersSevenArbitrary.m
288 lines (217 loc) · 10.4 KB
/
WithPreFiltersSevenArbitrary.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
clear all;
close all;
clc;
%---------------------------Few initializations---------------------------%
T = 0.001;
a = 0;
b = 1;
t= [a:T:b];
%-------------------------------------------------------------------------%
%--------------------------Speficied Parameters for the signal----------%
N = length(t);
K = 7;
A = .5 + (1)*rand(1,K);
phi = pi* rand(1,K);
f = [78, 137, 177, 234, 288, 325, 437];
PureSignal = zeros (1,N);
Signals = zeros (K,N);
for i =1: K
Signals(i,:) = A(i)*sin(2*pi*f(i)*t + phi(i));
PureSignal = PureSignal + Signals(i,:);
end
SNRdB = 10;
Y =awgn(PureSignal,SNRdB,'measured'); % infecting the signal with noise
linewidth = 2.5;
figure,
hold on
plot(t, Y,'-.g','LineWidth', linewidth-.5)
plot(t, Y - PureSignal, '-.', 'LineWidth', linewidth-1)
hold off
ylabel ('Amplitude (units)')
xlabel('Time(seconds)')
noise = strcat('Noise,SNR=', num2str(SNRdB), ' dB')
legend('Composite',noise)
grid on
% print -depsc2 SevenSignals_Noise
colors = ['b','g','r','c','m','y','k'];
figure,
hold on
for i =1:K
plot (t,Signals(i,:), colors(i), 'LineWidth', linewidth);
end
hold off
axis ([.0 .03 -1.9 1.9])
str1 = strcat(num2str(f(1)), ' Hz');
str2 = strcat(num2str(f(2)), ' Hz');
str3 = strcat(num2str(f(3)), ' Hz');
str4 = strcat(num2str(f(4)), ' Hz');
str5 = strcat(num2str(f(5)), ' Hz');
str6 = strcat(num2str(f(6)), ' Hz');
str7 = strcat(num2str(f(7)), ' Hz');
ylabel ('Amplitude (units)')
xlabel('Time(seconds)')
legend( str1,str2,str3,str4,str5,str6,str7)
grid on
% print -depsc2 DecomSevenSignals_Noise
figure, stem (f, A, 'LineWidth', 2.3)
xlabel('Frequency (Hz)')
ylabel ('Amplitude (units)')
grid on
initialFreqs = [70, 130, 180, 240, 270, 330, 450]; % Narrow BandPass center initial Frequencies
%----------------------Prefiltered Configuration -------------------------%
% Passband +- 10% from initial center frequency %
% Stopband +- 15% from initial center frequency %
Fs = 1/T; % 1 kHz sampling frequency as defined in the signal
d=fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2' , initialFreqs(1)-.15* initialFreqs(1) , initialFreqs(1)-.1* initialFreqs(1) ,initialFreqs(1) + .12* initialFreqs(1), initialFreqs(1) + .18* initialFreqs(1), 90,2,90, Fs);
% designmethods(d);
Hd1 = design(d,'equiripple');
d=fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2' , initialFreqs(2)-.15* initialFreqs(2) , initialFreqs(2)-.1* initialFreqs(2) ,initialFreqs(2) + .1* initialFreqs(2), initialFreqs(2) + .15* initialFreqs(2), 90,2,90, Fs);
Hd2 = design(d,'equiripple');
d=fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2' , initialFreqs(3)-.15* initialFreqs(3) , initialFreqs(3)-.1* initialFreqs(3) ,initialFreqs(3) + .1* initialFreqs(3), initialFreqs(3) + .15* initialFreqs(3), 90,2,90, Fs);
Hd3 = design(d,'equiripple');
d=fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2' , initialFreqs(4)-.15* initialFreqs(4) , initialFreqs(4)-.1* initialFreqs(4) ,initialFreqs(4) + .1* initialFreqs(4), initialFreqs(4) + .15* initialFreqs(4), 90,2,90, Fs);
Hd4 = design(d,'equiripple');
d=fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2' , initialFreqs(5)-.1* initialFreqs(5) , initialFreqs(5)-.08* initialFreqs(5) ,initialFreqs(5) + .08* initialFreqs(5), initialFreqs(5) + .15* initialFreqs(5), 90,2,90, Fs);
Hd5 = design(d,'equiripple');
d=fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2' , initialFreqs(6)-.04* initialFreqs(6) , initialFreqs(6)-.02* initialFreqs(6) ,initialFreqs(6) + .02* initialFreqs(6), initialFreqs(6) + .04* initialFreqs(6), 90,2,90, Fs);
Hd6 = design(d,'equiripple');
d=fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2' , initialFreqs(7)-.1* initialFreqs(7) , initialFreqs(7)-.04* initialFreqs(7) ,initialFreqs(7) + .04* initialFreqs(7), initialFreqs(7) + .1* initialFreqs(7), 90,2,90, Fs);
Hd7 = design(d,'equiripple');
h = fvtool(Hd1,Hd2, Hd3, Hd4, Hd5, Hd6, Hd7 ); % get the handle to the graphics
set(h,'DesignMask','off'); % Turn off design mask
hchildren = get(h,'children');
haxes = hchildren(strcmpi(get(hchildren,'type'),'axes'));
hline = get(haxes,'children');
set(hline,'LineWidth', 1.8)
str1 = strcat('Bandpass, f_1(center) = ', num2str(initialFreqs(1)), 'Hz');
str2 = strcat('Bandpass, f_2(center) = ', num2str(initialFreqs(2)), 'Hz');
str3 = strcat('Bandpass, f_3(center) = ', num2str(initialFreqs(3)), 'Hz');
str4 = strcat('Bandpass, f_4(center) = ', num2str(initialFreqs(4)), 'Hz');
str5 = strcat('Bandpass, f_5(center) = ', num2str(initialFreqs(5)), 'Hz');
str6 = strcat('Bandpass, f_6(center) = ', num2str(initialFreqs(6)), 'Hz');
str7 = strcat('Bandpass, f_7(center) = ', num2str(initialFreqs(7)), 'Hz');
legend (str1,str2, str3, str4, str5, str6, str7);
NewY= filter(Hd1,Y); % Prefiltering
% figure, plot(t, Y, t , NewY)
% xlabel('t(sec)')
% ylabel('A(units)')
% legend('Signal', 'Filtered in 70 Hz range')
gamma = 0.001;
xi = .15;
[X1, X2,X13]= NonUniform4thOrderANFFixedBlock(NewY, t, 2*pi*initialFreqs(1), gamma, xi);
NewY= filter(Hd2,Y); % Prefiltering
gamma = 0.001;
xi = .15;
[X1, X2,X23]= NonUniform4thOrderANFFixedBlock(NewY, t, 2*pi*initialFreqs(2), gamma, xi);
NewY= filter(Hd3,Y); % Prefiltering
gamma = 0.001;
xi = .15;
[X1, X2,X33]= NonUniform4thOrderANFFixedBlock(NewY, t, 2*pi*initialFreqs(3), gamma, xi);
NewY= filter(Hd4,Y); % Prefiltering
gamma = 0.001;
xi = .015;
[X1, X2,X43]= NonUniform4thOrderANFFixedBlock(NewY, t, 2*pi* initialFreqs(4), gamma, xi);
%----------------------Using High Frequency estimation Trick for the other 3 freq--------------%
%-----------------------------Estimating f(5)---------------------------%
% figure, plot(t, Y);
NewY= filter(Hd5,Y); % Prefiltering
% figure, plot(t, NewY);
deltaf = 70; % select offset
f_c1 = initialFreqs(5)+deltaf;
Y_Sine = NewY .* sin(2*pi*f_c1* t); % Sine Modulate
Y_Cosine = NewY .* cos(2*pi*f_c1* t); % Cosine Modulate
d=fdesign.lowpass('Fp,Fst,Ap,Ast', deltaf, deltaf+10,1,90, Fs);
Hd = design(d,'equiripple');
h = fvtool(Hd); % get the handle to the graphics
set(h,'DesignMask','off'); % Turn off design mask
hchildren = get(h,'children');
haxes = hchildren(strcmpi(get(hchildren,'type'),'axes'));
hline = get(haxes,'children');
set(hline,'LineWidth', 1.8)
legend('lowpass filter')
% figure, plot(t, Y_Sine)
Y_Sine = filter(Hd,Y_Sine); % Apply Lowpass 1 stage
% figure, plot(t, Y_Sine)
Y_Cosine = filter(Hd,Y_Cosine); % Apply Lowpass 2 stage
A_Estimated = sum( sqrt( (2*Y_Sine).^2 + (2* Y_Cosine).^2) )/ length(t);
Y_Sine = Y_Sine / A_Estimated * 1.5;
% figure, plot(t,Y_Sine)
gamma = 0.001;
xi = .15;
[X1, X2,X53]= NonUniform4thOrderANFFixedBlock(Y_Sine, t, 2*pi*deltaf, gamma, xi);
%------------------------------------------------------------------------%
%--------------------------Estimating f(6)-----------------------------%
NewY= filter(Hd6,Y); % Prefiltering
% figure, plot(t, NewY);
deltaf = 80; % select offset
f_c2 = initialFreqs(6)+deltaf;
Y_Sine = NewY .* sin(2*pi*f_c2* t); % Sine Modulate
Y_Cosine = NewY .* cos(2*pi*f_c2* t); % Cosine Modulate
d=fdesign.lowpass('Fp,Fst,Ap,Ast', deltaf, deltaf+10,1,90, Fs);
Hd = design(d,'equiripple');
h = fvtool(Hd); % get the handle to the graphics
set(h,'DesignMask','off'); % Turn off design mask
hchildren = get(h,'children');
haxes = hchildren(strcmpi(get(hchildren,'type'),'axes'));
hline = get(haxes,'children');
set(hline,'LineWidth', 1.8)
legend('lowpass filter')
% figure, plot(t, Y_Sine)
Y_Sine = filter(Hd,Y_Sine); % Apply Lowpass 1 stage
% figure, plot(t, Y_Sine)
Y_Cosine = filter(Hd,Y_Cosine); % Apply Lowpass 2 stage
A_Estimated = sum( sqrt( (2*Y_Sine).^2 + (2* Y_Cosine).^2) )/ length(t);
Y_Sine = Y_Sine / A_Estimated * 2;
% figure, plot(t,Y_Sine)
gamma = 0.001;
xi = .15;
[X1, X2,X63]= NonUniform4thOrderANFFixedBlock(Y_Sine, t, 2*pi*deltaf, gamma, xi);
%-------------------------------------------------------------------------%
%--------------------------Estimating f(7)-----------------------------%
% figure, plot(t, Y) % Observe Before
NewY= filter(Hd7,Y); % Prefiltering
% figure, plot(t, NewY) % Observe After
deltaf = 100; % select offset
f_c3 = initialFreqs(7)+deltaf;
Y_Sine = NewY .* sin(2*pi*f_c3* t); % Sine Modulate
Y_Cosine = NewY .* cos(2*pi*f_c3* t); % Cosine Modulate
% d=fdesign.lowpass('Fp,Fst,Ap,Ast', deltaf, deltaf+10,1,90, Fs); % Dont use lowpass because of aliased frequency about 12 Hz
% Hd = design(d,'equiripple');
% h = fvtool(Hd); % get the handle to the graphics
% set(h,'DesignMask','off'); % Turn off design mask
% hchildren = get(h,'children');
% haxes = hchildren(strcmpi(get(hchildren,'type'),'axes'));
% hline = get(haxes,'children');
% set(hline,'LineWidth', 1.8)
% legend('lowpass filter')
%
% % figure, plot(t, Y_Sine)
% Y_Sine = filter(Hd,Y_Sine); % Apply Lowpass 1 stage
% % figure, plot(t, Y_Sine)
% Y_Cosine = filter(Hd,Y_Cosine); % Apply Lowpass 2 stage
A_Estimated = sum( sqrt( (2*Y_Sine).^2 + (2* Y_Cosine).^2) )/ length(t);
Y_Sine = Y_Sine / A_Estimated * 1.5;
% figure, plot(t,Y_Sine)
gamma = 0.001;
xi = .015;
[X1, X2,X73]= NonUniform4thOrderANFFixedBlock(Y_Sine, t, 2*pi*deltaf, gamma, xi);
%-------------------------------------------------------------------------%
unitStep = ones(1,N);
figure, plot(t, unitStep* f(1),t, unitStep* f(2),t, unitStep* f(3),t, unitStep* f(4), t,X13/(2*pi), '-.', t,X23/(2*pi), '-.', t,X33/(2*pi), '-.', t,X43/(2*pi), '-.', 'LineWidth', linewidth)
title('(a)')
% axis tight
% title ('Prefiltered Configuration')
Ax = legend('True freq 1','True freq 2','True freq 3','True freq 4', 'Estimate 1','Estimate 2','Estimate 3','Estimate 4' )
Ax.FontSize = 14;
ylabel('Frequency (Hz)')
xlabel('Time (seconds)')
grid on
% print -depsc2 EstimatesForFiveArbitrary
%-------------------------------------------------------------------------%
figure, plot (t, unitStep* (f_c1 - f(5)),t, unitStep* (f_c2 - f(6)),t, unitStep* (f_c3 - f(7)), t,X53/(2*pi), '-.', t,X63/(2*pi),'-.',t,X73/(2*pi),'-.', 'LineWidth', linewidth)
title('(b)')
ylabel ('Frequency (Hz)')
xlabel('Time (seconds)')
Ax = legend('True freq offset 1','True freq offset 2', 'True freq offset 3', 'Estimate offset 1','Estimate offset 2', 'Estimate offset 3')
Ax.FontSize = 14;
grid on