-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreqShape.asv
104 lines (83 loc) · 2.6 KB
/
FreqShape.asv
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
%Frequency Shaping Filter
function y = FreqShape(x,g_max,tr_1,tr_2,tr_3,tr_4,fsr)
% Creates the gain filter for a patient with ski slope hearing loss
%
% The maximum gain will be g and the minimum gain will be one. The magnitude
% of gain function will be the concatenation of preset piecewise functions
% The transition frequencies from one piecewise function to another can
% be set by the user in the elements of the tr_1,2,3,4
% The output will be the filtered signal
% x - an input sound signal
% g_max - the maximum gain that will be applied to the signal
% tr_1,2,3,4 - 4 frequency values where the gain changes to the next piecewise function
% determined by an audiologist, that define the user's hearing characteristics.
% For each range, the frequency shaper applies a certain gain based on the user's specific hearing loss
% fsr - the sampling frequency of the input signal
x_length = length(x);
%nextpow2(x) : the next power of two greater than or equal to its value
n = nextpow2(x_length);
N = 2^n;
T = 1/fsr;
% returns the N point DFT
X = fft(x,N);
gain = zeros(N,1);
% Sets the gain for the first stage of frequencies
firstC = (.3*(g_max-1))/tr_1;
k=0;
while(k/N <= tr_1/fsr) % k
gain(k+1) = firstC*k/(N*T) + 1;
gain(N-k) = gain(k+1);
k=k+1;
end
% Sets the gain for the second stage of frequencies
secondC = firstC*tr_1 +1;
secondC2 = (tr_2-tr_1)/5;
while(k/N <= tr_2/fsr) %
gain(k+1) = 1 + (secondC-1)*exp(-((k/(N*T))-tr_1)/secondC2);
gain(N-k) = gain(k+1);
k=k+1;
end
% Sets the gain for the third stage of frequencies
thirdC = 1 + (secondC-1)*exp(-tr_2/secondC2);
thirdC2 = (tr_3-tr_2)/5;
while(k/N <= tr_3/fsr)
gain(k+1) = g_max + (thirdC-g_max)*exp(-((k/(N*T)-tr_2))/thirdC2);
gain(N-k) = gain(k+1);
k=k+1;
end
% Sets the gain for the fourth stage of frequencies
while(k/N <= tr_4/fsr)
gain(k+1) = g_max;
gain(N-k) = gain(k+1);
k=k+1;
end
% Sets the gain for the fifth stage of frequencies
fifthC = g_max;
fifthC2 = (fsr/2-tr_4)/5;
while(k/N <= .5)
gain(k+1) = 1 + (fifthC-1)*exp(-((k/(N*T))-tr_4)/fifthC2);
gain(N-k) = gain(k+1);
k=k+1;
end
k_v = (0:N-1)/N;
plot(k_v,gain);
title('Gain');%entire filter transfer function
figure;%non-redundant filter transfer function
k_v = k_v*fsr;
k_v = k_v(1:N/2+1);
plot(k_v,gain(1:N/2+1));
title('Frequency Shaper Transfer Function');
xlabel('Frequency (Hertz)');
ylabel('Gain');
xlim([0 10000]);
Y = X+gain;
y = real(ifft(Y,N));
y = y(1:x_length);
t=(0:1/fsr:(x_length-1)/fsr);
figure;
subplot(2,1,1);
plot(t,y,'r');
title('Signal after addition of gain');
subplot(2,1,2);
plot(t,x);
title('Adjusted Signal');