-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main_Event_Loop.m
181 lines (153 loc) · 7.69 KB
/
Main_Event_Loop.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
function Main_Event_Loop(dspIQ_RX, dspAudioIn, dspIQ_TX, dspAudioOut, params)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
global F_radio;
global F_os;
global Ftune;
global updateIF;
global updateRadioFreq;
global quit;
global PTT;
global mode;
global newMode;
global noiseFilter;
noiseFilter = 'None';
mode = 'USB';
newMode = false;
PTT = false;
quit = false;
updateIF = false;
updateRadioFreq = false;
srSetFreq(params.F_radio_initial, params.SR_DLL_NAME);
F_radio = srGetFreq(params.SR_DLL_NAME);
F_os = 20e3;
F_IF_BW = params.F_IF_HighFreqCutOff - params.F_IF_LowFreqCutOff;
Ftune = F_radio + F_os;
F_IF = Ftune-F_radio;
[gui_text_freq] = CreateGUI(params);
%% Setup filers
hBaseBandSpectrum = dsp.SpectrumAnalyzer('FrequencyOffset', 0,...
'PlotAsTwoSidedSpectrum', true,...
'SampleRate', params.Fs_audio,...
'SpectralAverages', params.SpecAverages,...
'FrequencyResolutionMethod', 'RBW',...
'RBWSource', 'Property',...
'RBW', params.RBW,...
'Title', 'Base Band Power Spectrum',...
'YLimits', [-80 0]);
hWaterfall = dsp.SpectrumAnalyzer('FrequencyOffset', F_radio,...
'PlotAsTwoSidedSpectrum', true,...
'SampleRate', params.Fs_radio,...
'SpectralAverages', params.SpecAverages,...
'FrequencyResolutionMethod', 'RBW',...
'RBWSource', 'Property',...
'RBW', params.RBW,...
'SpectrumType', 'Spectrogram',....
'Title', 'Radio Power Spectrum',...
'TimeResolutionSource', 'Property',...
'TimeResolution', params.WaterfallRate);
hSpectrum = dsp.SpectrumAnalyzer('FrequencyOffset', F_radio,...
'PlotAsTwoSidedSpectrum', true,...
'SampleRate', params.Fs_radio,...
'SpectralAverages', params.SpecAverages,...
'FrequencyResolutionMethod', 'RBW',...
'RBWSource', 'Property',...
'RBW', params.RBW,...
'Title', 'Radio Power Spectrum',...
'YLimits', [-80 0]);
hSRC = dsp.SampleRateConverter('Bandwidth', params.Fs_audio/2,...
'InputSampleRate', params.Fs_radio,...
'OutputSampleRate', params.Fs_audio);
% Design IF filter
% This is a complex filter that is not symmetric about DC
% Start with a low pass filter, then complex frequency shift to desired
% center frequency.
Fp = 0.1; % Doesn't seem to do anything
Fc = F_IF/(params.Fs_radio/2); % Frequency shift of low pass
[b,a] = ellip(7,0.2,80,F_IF_BW/params.Fs_radio);
[Num,Den,~,~] = iirlp2bpc(b, a, Fp, [Fc-Fp, Fc+Fp]); % Shift filter response
dspIFfilt = dsp.IIRFilter; % Create DSP object
dspIFfilt.Numerator = Num; % Load in new parameters
dspIFfilt.Denominator = Den;
% Hilbert transform filter (90 deg phase shift)
N_hilbert_order = 100;
hHilbertDesign = design(fdesign.hilbert('N,TW',N_hilbert_order,0.025), 'equiripple');
%fvtool(hHilbert,'magnitudedisplay','zero-phase','frequencyrange','[-pi, pi)')
hHilbert = dsp.FIRFilter;
hHilbert.Numerator = hHilbertDesign.Numerator;
% Delay to counteract the Hilbert filter
hDelay = dsp.Delay(N_hilbert_order/2);
%% Main signal processing loop
tvec = linspace(0, (params.SAMPLES_PER_FRAME_RADIO-1)/params.Fs_radio, params.SAMPLES_PER_FRAME_RADIO).';
% Keep track of frame number for time domain processing
t=0;
while quit == false
% Compute time vector for time-domain operations
Tt = t*params.SAMPLE_TIME+tvec;
if updateIF || updateRadioFreq
updateIF = false;
updateRadioFreq = false;
% Set radio frequency
srSetFreq(F_radio, params.SR_DLL_NAME);
% Get actual set frequency incase there is a discrepency
F_radio = srGetFreq(params.SR_DLL_NAME);
% Updating local frequencies
F_IF_BW = params.F_IF_HighFreqCutOff - params.F_IF_LowFreqCutOff;
Ftune = F_radio + F_os;
F_IF = Ftune-F_radio;
% Update spectrum analyzer
hSpectrum.set('FrequencyOffset', F_radio);
release(dspIFfilt);
% Regenerate IF filter
Fp = 0.1; % Doesn't seem to do anything
Fc = F_IF/(params.Fs_radio/2); % Frequency shift of low pass
[b,a] = ellip(7,0.2,80,F_IF_BW/params.Fs_radio);
[Num,Den,~,~] = iirlp2bpc(b, a, Fp, [Fc-Fp, Fc+Fp]); % Shift filter response
dspIFfilt = dsp.IIRFilter; % Create DSP object
dspIFfilt.Numerator = Num; % Load in new parameters
dspIFfilt.Denominator = Den;
% Update frequency readout
gui_text_freq.String = [num2str(1e-6*Ftune, '%#2.6g') ' MHz'];
end
% Grab new frame from radio IQ RX
sigIQ_RX = step(dspIQ_RX);
I_rx = sigIQ_RX(:,params.RX_I_CHAN);
Q_rx = sigIQ_RX(:,params.RX_Q_CHAN);
IQ_rx = I_rx - 1j*Q_rx;
% Apply IF bandpass filter
IQ_rx_IF = step(dspIFfilt, IQ_rx);
switch mode
case 'USB'
[rx_demod, IQ_rx_bb] = demodSSB(IQ_rx_IF, hDelay, hHilbert, hSRC, F_IF, F_IF_BW, params.F_IF_LowFreqCutOff, Tt, -1);
case 'LSB'
[rx_demod, IQ_rx_bb] = demodSSB(IQ_rx_IF, hDelay, hHilbert, hSRC, F_IF, F_IF_BW, params.F_IF_LowFreqCutOff, Tt, +1);
otherwise
error('Invalid mode!');
end
% Need to add Automatic Gain Control (AGC)
rx_demod = 5*rx_demod;
switch noiseFilter
case 'None'
case 'Wavelet'
rx_demod = wden(rx_demod,'sqtwolog','s','mln',4,'sym8');
case 'Savitzky-Golay'
rx_demod = sgolayfilt(rx_demod,7,81);
otherwise
error('Invalid noise reduction filter!');
end
% Output audio to headphones
Nur = step(dspAudioOut, repmat(rx_demod,1,2));
if (Nur > 100)
disp(['Lagging by ' num2str(Nur) ' samples.']);
end
% Visualize the signal
step(hWaterfall, IQ_rx);
step(hSpectrum, IQ_rx);
step(hBaseBandSpectrum, IQ_rx_bb);
% Increment frame number counter (time)
t=t+1;
end
%% Shut down gracefully
release(hWaterfall);
release(hSpectrum);
end