-
Notifications
You must be signed in to change notification settings - Fork 1
/
receiver.m
73 lines (59 loc) · 2.43 KB
/
receiver.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
% Script to Receiver
addpath('functions');
%% Global Variables
Fs=44100; %sampling frequency in Hz
Fc=4410; % carrier frequency in Hz
S=1000; %number of symbols per frame
L=20; %oversampling factor
wc=pi/2; %carrier frequency: 0.5*pi rad (or Fs/4 Hz)
M=4; %number of symbols in alphabet
b=log2(M); %num of bits per symbol
% n_bits=b*S; %total number of bits to be transmitted
n_bits = 10500;
rolloff=0.5; %roll-off factor for sqrt raised cosine
delay_symbols=3; %delay at symbol rate
en_channel = 1; % Flag to Enable Channel
channel_code_type = 'conv'; % 'conv' or 'linear'
img_name = 'images/5.1.10.tiff';
input_audio = './transmit_audio.wav';
img_number = 9; % 9, 10, 11, 12, 13 or 14
img_props = imgProps(img_number);
%% Receiver
% Reading audio received
y_signal = audioread(input_audio);
y_signal = y_signal(:)';
%Downconversion
rx_signal = downconversion(y_signal,4410, Fs);
% rx_signal = y_signal;
%Matched Filter
rx_signal_filt = matchedFilter(rx_signal, L,rolloff, delay_symbols);
% Symbol synchronization on the receiver side
% Furthemore, this function implements the downsampling to Rsym
txPreamble = [+1 +1 +1 +1 +1 -1 -1 +1 +1 -1 +1 -1 +1 ...
+1 +1 +1 +1 +1 -1 -1 +1 +1 -1 +1 -1 +1 ...
+1 +1 +1 +1 +1 -1 -1 +1 +1 -1 +1 -1 +1 ...
+1 +1 +1 +1 +1 -1 -1 +1 +1 -1 +1 -1 +1 ...
+1 +1 +1 +1 +1 -1 -1 +1 +1 -1 +1 -1 +1];
upsampled_txPreamble = upsample(txPreamble, L);
[c,lags] = xcorr(rx_signal_filt,upsampled_txPreamble);
plot(lags,c);
[rx_symbols, n_frames, rx_up_preamble] = timeSync(rx_signal_filt, S, ...
upsampled_txPreamble,L);
matrixLength = size(rx_up_preamble);
for j = 1:matrixLength(1)
gain_adjustment = mean(rx_up_preamble(j,1:end)./(2.2361*txPreamble));
rx_symbols((j-1)*S+1:S*j) = rx_symbols((j-1)*S+1:S*j)/gain_adjustment;
end
% Demodulating M-PAM symbols to cbitstream
rx_bitstream = pam2bin(rx_symbols,M);
% Channel Decoding
channel_code_tblen = n_frames*S;
decoded_rx_bitstream = channelDecoding(rx_bitstream(1:n_frames*S*log2(M)),...
channel_code_type,...
channel_code_tblen);
%BER = berEstimation(decoded_rx_bitstream, tx_bitstream);
% Source Decoding and recovering image
img = sourceDecoding(decoded_rx_bitstream(1:img_props.source_size));
% Calculating PSNR
ori_img = imread(img_name);
psnr_value = psnr(double(img), double(ori_img));