function rxObj = WIFI_RX(wifi_params,rxObj,channelObj, ChanMod_output, i_mcs)
%% WIFI_RX

%% DEFINE MODEL PARAMETERS

% define parameters of used MCS
MCS = wifi_params.MCS(i_mcs);

% define modulation parameters
hDemod = wifi_params.modulation(log2(MCS.M)).hDemod;
hDemod_LLR = wifi_params.modulation(log2(MCS.M)).hDemod_LLR;

hScalQuant = wifi_params.modulation.hScalQuant;

modNorm = wifi_params.modulation(log2(MCS.M)).hMod_factor;

n_fft = wifi_params.mapping.n_fft;
n_tot = wifi_params.mapping.n_tot;

Nsym = wifi_params.mapping.n_data_symbols_per_frame;
% n_sf = wifi_params.coexistence.N_available_symbols;

n_cp = wifi_params.cprefix.n_cp;

LENGTH = wifi_params.general.LENGTH;
service_length = wifi_params.mapping.service_length;

N_sym = wifi_params.coding.N_sym;% y_rx = channelObj.x_hn;

y_rx = ChanMod_output;

%What i need - J146
new_N_sym = wifi_params.general.newNsym;
%pathGains = channelObj.pathGains;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% y_rx = y_rx(1:wifi_params.coexistence.N_available_symbols*(wifi_params.mapping.n_fft+wifi_params.cprefix.n_cp));
%% received vector length adaptation
if strcmp(wifi_params.channel.type,'fading') % asi bude potreba prepracovat az budu resit simulaci fadingu!!
    y_r = y_rx(1:end-channelObj.Lch+1);
elseif strcmp(wifi_params.channel.type,'fad')
    y_r = y_rx(:,1:end-channelObj.Lch+1);
    y_r = y_r.';
else
    addSomeNoise = wifi_params.general.addSomeNoise;
    if (strcmp(addSomeNoise,'fadTest'))
        h_completPath = channelObj.h_completPath;
        y_rx = y_rx./h_completPath;
        y_r = y_rx.';
    else
        y_r = y_rx.';
    end
end

    
%if (wifi_params.general.addSomeNoise == true)
    for i_amountOfAntennas = 1:wifi_params.general.N_sts
        y_cp_ifft(:,:,i_amountOfAntennas) = reshape(y_r(:,i_amountOfAntennas),[],new_N_sym);
        %y_cp_ifft(:,:,i_amountOfAntennas) = reshape(y_r(:,i_amountOfAntennas),[],Nsym);
    end
    
    
    
%else
    
    
%end
%% remove cyclic prefix

for i_amountOfAntennas = 1:wifi_params.general.N_sts
    
    y_ifft(:,:,i_amountOfAntennas) = y_cp_ifft(n_cp+1:end,:,i_amountOfAntennas);

    y_shifted(:,:,i_amountOfAntennas) = (sqrt(n_tot)/n_fft)*fft(y_ifft(:,:,i_amountOfAntennas),n_fft);
    y_dc_padded(:,:,i_amountOfAntennas) = circshift(y_shifted(:,:,i_amountOfAntennas),-ceil((n_fft-n_tot-numel(wifi_params.mapping.i_DC))/2));
    
end

%% estimation
%         rx_pilots = y_dc_padded([7 22 36 51],:);
%         inv_rx_pilots = rx_pilots.^(-1)
%         realDif = real(mod_pilots-rx_pilots);
%         imagDif = imag(mod_pilots-rx_pilots);
%
%         for i_subf = 1:n_sf
%             odhad(:,:,i_subf) = diag(ones(1,64));
%         end

switch wifi_params.general.eqType
    case 'ideal'
%         if (strcmp(wifi_params.channel.type,'fad'))
            for i_channelPath = 1:wifi_params.general.N_sts
                H_rx(:,:,i_channelPath) = channelObj.H(:,:,i_channelPath,i_channelPath); % perfect channel parameter knowledge
            end
            H_rx_shifted = circshift(H_rx,-ceil((n_fft-n_tot-numel(wifi_params.mapping.i_DC))/2));
            H_rx_shifted = repmat(H_rx_shifted,1,new_N_sym);
    
            % equalization
            y_equalized = y_dc_padded./H_rx_shifted; 
%     
%         else
% 
%             y_equalized = y_dc_padded;
%         end
    otherwise
        y_equalized = y_dc_padded;
 
 end
%% pick-up data + pilot + DC carriers
y_rx_dc_pilots = y_equalized(1:n_tot+numel(wifi_params.mapping.i_DC),:,:,:);

%% pick up and reshape received data
mapData = wifi_params.mapping.map_data(:,1);
mapPilots = wifi_params.mapping.map_pilots(:,1);

mapData = repmat(mapData,1,new_N_sym);
mapPilots = repmat(mapPilots,1,new_N_sym);

% inverse phase rotation
phase_rot_temp = ones(size(y_rx_dc_pilots));
phase_rot_temp(repmat(wifi_params.mapping.map_phase_rotation(:,1),1,new_N_sym)) = conj(wifi_params.mapping.phase_rotation_factor);
y_rx_dc_pilots = y_rx_dc_pilots.*phase_rot_temp;


for i_amountOfAntennas = 1:wifi_params.general.N_sts
    
    y_rx_tranmission = y_rx_dc_pilots(:,:,i_amountOfAntennas);
    
    y_rx_pilots = y_rx_tranmission(mapPilots);
    y_rx_pilots_adjusted = reshape(y_rx_pilots,1,[]);
    y_rx_pilots_adjusted_out(i_amountOfAntennas,:)=y_rx_pilots_adjusted;
    
    y_rx_data = y_rx_tranmission(mapData);
    y_rx_data_adjusted = reshape(y_rx_data,1,[]);
    y_rx_data_adjusted_out(i_amountOfAntennas,:)=y_rx_data_adjusted;

end
%         x = zeros(size(y_rx));
%         x2 = zeros(size(y_rx));
%         % demodulace
%         x(y_rx > -real(y_rx)) = 1;
%         x2(y_rx < -real(y_rx)) = 1;
% a = wifi_params.modulation(log2(MCS.M)).hMod.constellation*wifi_params.modulation(log2(MCS.M)).hMod_factor;
% plot(y_rx_data_adjusted,'.b');
% hold on, grid on
% axis square, axis([-1.5 1.5 -1.5 1.5])
% plot(a,'+r','MarkerSize',8,'LineWidth',2)
% xlabel('In-phase')
% ylabel('Quadrature')
%% Equalization without knowing channel


switch wifi_params.general.eqType
    case 'linear'
        
        
        for i_amountOfAntennas = 1:wifi_params.general.N_sts
            eq_pilots = reshape(y_rx_pilots_adjusted_out(i_amountOfAntennas,:),wifi_params.mapping.n_pilot,[]);
            %equalization = (wifi_params.mapping.pilots_value.').*eq_pilots(:,1);
            equalization = eq_pilots(:,1);
            equalization = equalization.';

            Poly = [];
            
            %Coefficients calculation
            for i = 1:length(wifi_params.mapping.i_pilots)-1
               Poly(i+1,:) = polyfit(wifi_params.mapping.i_pilots(i:i+1),equalization(i:i+1),1);
            end

            %Add first and last coefficients
            Poly(1,:)= Poly(2,:);
            Poly(end+1,:)=Poly(end,:);
            %Poly(end,:)
            
            %H calculation
            index_eq = 1;
            H_eq_1 = [];

            for i = 1:length(wifi_params.mapping.i_pilots)
               H_eq_i =  polyval(Poly(i,:),index_eq:wifi_params.mapping.i_pilots(i));
               index_eq = wifi_params.mapping.i_pilots(i)+1;
               H_eq_1 = [H_eq_1 H_eq_i];
            end

            H_eq_2 =  polyval(Poly(end,:),wifi_params.mapping.i_pilots(end)+1:wifi_params.mapping.i_data(end));
            H_eq = [H_eq_1 H_eq_2];
            H_eq = H_eq.';

            H_rx_eq = H_eq(mapData(:,1));
            H_rx_eq = repmat(H_rx_eq,new_N_sym,1);
            H_rx_eq = H_rx_eq.';
            H_out(i_amountOfAntennas,:) = H_rx_eq;

            
            for i_channelPath = 1:wifi_params.general.N_sts
                H_rx(:,:,i_channelPath) = channelObj.H(:,:,i_channelPath,i_channelPath); % perfect channel parameter knowledge
            end
            H_rx_shifted = circshift(H_rx,-ceil((n_fft-n_tot-numel(wifi_params.mapping.i_DC))/2));
            H_rx_shifted = repmat(H_rx_shifted,1,new_N_sym);
            p_poly = polyfit(wifi_params.mapping.i_pilots,equalization,3);
            H_poly = polyval(p_poly,wifi_params.mapping.i_data);
              
            figure
            set(gca,'fontsize',25)
            lgnd = 'Přijatá data';
            plot(1:57,y_rx_tranmission(:,1),'Color','k','LineWidth',2,'MarkerSize',6,'DisplayName',lgnd)
            hold on
            lgnd = 'Původní nosné';
            stem(wifi_params.mapping.i_pilots,wifi_params.mapping.pilots_value,'Color','r','LineWidth',2,'MarkerSize',6,'DisplayName',lgnd)
            lgnd = 'Přijaté nosné';
            stem(wifi_params.mapping.i_pilots,equalization,'DisplayName',lgnd,'LineWidth',2,'MarkerSize',6)
            lgnd = 'Ideální frekvenční odezva';
            plot(1:64,H_rx_shifted(:,1,1),'Color','g','DisplayName',lgnd,'LineWidth',2,'MarkerSize',6)
            lgnd = 'Polynomická frekvenční odezva';
            plot(wifi_params.mapping.i_data,H_poly,'Color','r','DisplayName',lgnd,'LineWidth',2,'MarkerSize',6)
            lgnd = 'Frekvenční odezva proložením lomenou čárou';
            plot(1:length(H_eq),H_eq,'Color','c','DisplayName',lgnd,'LineWidth',2,'MarkerSize',6)
            lgnd = legend('show');
           % axis([1 57 -5 5])
            grid on
            
        end

        y_rx_data_adjusted_out = y_rx_data_adjusted_out./H_out;
        
    case 'polynom'
            
            for i_amountOfAntennas = 1:wifi_params.general.N_sts
            eq_pilots = reshape(y_rx_pilots_adjusted_out(i_amountOfAntennas,:),wifi_params.mapping.n_pilot,[]);
            equalization = (wifi_params.mapping.pilots_value.').*eq_pilots(:,1);
            equalization = equalization.';

            Poly = polyfit(wifi_params.mapping.i_pilots,equalization,3);
            
            H_rx_eq = polyval(Poly,wifi_params.mapping.i_data);
            
            H_rx_eq = repmat(H_rx_eq.',new_N_sym,1);
            H_rx_eq = H_rx_eq.';
            H_out(i_amountOfAntennas,:) = H_rx_eq;
            end
            y_rx_data_adjusted_out = y_rx_data_adjusted_out./H_out;
            
    otherwise 
        y_rx_data_adjusted_out = y_rx_data_adjusted_out;
        
end



%% SPATIAL DEMAPPING

spatialDemappingIn = y_rx_data_adjusted_out;
spatialDemappingOut = spatialDemappingIn;

%% CSD per STS    

    CSDIn = spatialDemappingOut.';
    
    for i_csd = 1:wifi_params.general.N_sts
        
    CSDOut(:,i_csd) = circshift(CSDIn(:,i_csd),round(-(wifi_params.mapping.csd_per_sts(i_csd)*wifi_params.Fs)));
    end
    



%% STBC DEMODULATION

    OSTBCDecIn = CSDOut;

    
if wifi_params.general.includeSTBC == true
    
    chEst = ones([length(y_rx_data_adjusted_out.'),2,2])./2;
    STBCDecOut = [];
    
    hOSTBCDec = comm.OSTBCCombiner;
    release(hOSTBCDec);
    hOSTBCDec.NumReceiveAntennas = 2; 
    hOSTBCDec.NumTransmitAntennas = 2; 
    
    for i_STBC = 1:2:wifi_params.general.N_sts
        STBCDecTransfer = OSTBCDecIn (:, i_STBC:i_STBC+1);

        STBCDecoded = step (hOSTBCDec, STBCDecTransfer, chEst);
        
        STBCDecOut = [STBCDecOut STBCDecoded];
    end
    
else
    
    STBCDecOut = OSTBCDecIn;
    
end
    


%% DEMODULATION
for i_amountOfStreams = 1:wifi_params.MCS(i_mcs).N_ss
    demodulationIn = STBCDecOut(:,i_amountOfStreams);
    demodulationIn = demodulationIn.';
    
    if strcmp(wifi_params.coding.decision_type,'LLR')
        %     hDemod_LLR.Variance = 10^(-channelObj.SNR/10);
        demodulatorOut(:,i_amountOfStreams) = step(hDemod_LLR, demodulationIn.'/modNorm);
        demodulatorOut_hard(:,i_amountOfStreams) = step(hDemod, demodulationIn.'/modNorm);
    else
        demodulatorOut(:,i_amountOfStreams) = step(hDemod, demodulationIn(i_amountOfStreams,:).'/modNorm);
    end
    
    
end
% demodSignal_gross = demodSignal<0;

% konstelace = wifi_params.MCS.hdemod.Constellation;
% figure
% plot(real(y_rx_adjusted*modNorm),imag(y_rx_adjusted*modNorm),'.');
% hold on
% plot(konstelace,'r+')
% axis([-7 7 -7 7])
% axis square


%% DEINTERLEAVING
for i_amountOfStreams = 1:wifi_params.MCS(i_mcs).N_ss
    deinterleaver_transmission = demodulatorOut(:,i_amountOfStreams);

    switch wifi_params.coding.type
        case 'BCC'
            deinterleaverIn = reshape(deinterleaver_transmission,wifi_params.interleaving.N_cbpss,[]);
            %
            % inverse third permutation (only if Nss > 1)
            if wifi_params.MCS(i_mcs).N_ss > 10
                inv_permOut(wifi_params.interleaving.r{1}+1,:) = deinterleaverIn;
            else
                inv_permOut = deinterleaverIn;
            end
            % inverse second permutation
            inv_permOut(wifi_params.interleaving.j{1}+1,:) = inv_permOut;
            % % inverse first permutation
            inv_permOut(wifi_params.interleaving.i{1}+1,:) = inv_permOut;
            deinterleaverOut(:,i_amountOfStreams) = reshape(inv_permOut,[],1);


        case 'LDPC'
            deinterleaverOut = demodulatorOut;
    end

end
%% STREAM DEPARSING  (N_ss -> N_es)
    
    streamDeparsingIn = deinterleaverOut;
    streamDeparsingTransfer = [];
   
%     for i_Nss = 1:wifi_params.MCS(i_mcs).N_ss
%         
%     streamDeparsingTransfer = [streamDeparsingTransfer; streamDeparsingIn(:,i_Nss)];
%     end
%     
%     n_streamsParsed = length(streamDeparsingTransfer)/wifi_params.MCS(i_mcs).N_es;
%     
%     for i_Nes = 1:wifi_params.MCS(i_mcs).N_es
%         streamDeparsingOut(:,i_Nes) = streamDeparsingTransfer(n_streamsParsed*(i_Nes-1)+1:n_streamsParsed*i_Nes);
%     end
    
    s_parser = max([(log2(MCS.M))/2, 1]);
    S_parser = MCS.N_ss*s_parser;
    
    streamDeparsingTransfer2 = [];

    for i = 1:MCS.N_ss
        streamDeparsingTransfer = [ streamDeparsingTransfer; reshape(streamDeparsingIn(:,i),s_parser,[]) ];

    end

    streamDeparsingTransfer2 = reshape(streamDeparsingTransfer,[],1);
    streamDeparsingTransfer2 = reshape(streamDeparsingTransfer2,S_parser,[]);

    for i = 1:MCS.N_es
        streamDeparsingOut2(:,i) = reshape(streamDeparsingTransfer2(:,i:MCS.N_es:end),[],1);
    end


%% CHANNEL DECODING
for i_amountOfStreams = 1:MCS.N_es

    decoderIn = streamDeparsingOut2(:,i_amountOfStreams);
    switch wifi_params.coding.type
        case 'BCC'
            %% BCC %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            if MCS.N_es == 1
                DEC_offset = wifi_params.coding.tail_length;
            elseif MCS.N_es == 2
                DEC_offset = 2*wifi_params.coding.tail_length;
            else
                error('Wrong number of BCC encoders');
            end

            switch MCS.CR
                case 1/2
                    puncpat = wifi_params.coding.puncturing(1).pattern;
                case 2/3
                    puncpat = wifi_params.coding.puncturing(2).pattern;
                case 3/4
                    puncpat = wifi_params.coding.puncturing(3).pattern;
                case 5/6
                    puncpat = wifi_params.coding.puncturing(4).pattern;
            end
            trellis = wifi_params.coding.trellis;
            if ~istrellis(trellis)
                error('Wrong trellis structure');
            end

            switch wifi_params.coding.decision_type
                case 'HD'
                    hDec = comm.ViterbiDecoder(trellis,...
                        'InputFormat','Hard',...
                        'PuncturePatternSource','Property',...
                        'PuncturePattern',puncpat.');
                    hDec.TracebackDepth = wifi_params.coding.tail_length;

                    decoderOut(:,i_amountOfStreams) = step(hDec,decoderIn); % hard decoding [TAIL, SERVICE, DATA, PAD_BITS]
                case 'LLR'
                    %                 hDec_SD = comm.ViterbiDecoder(trellis,...
                    %                     'InputFormat','Soft',...
                    %                     'SoftInputWordLength', log2(numel(wifi_params.modulation(log2(MCS.M)).bound_pts_wo_noiseVar)+1),...
                    %                     'PuncturePatternSource','Property',...
                    %                     'PuncturePattern',puncpat.');
                    hDec_SD = comm.ViterbiDecoder(trellis,...
                        'InputFormat','Unquantized',...
                        'PuncturePatternSource','Property',...
                        'PuncturePattern',puncpat.');
                    hDec_SD.TracebackDepth = wifi_params.coding.tail_length;

                    %                 NoiseVariance = 10^(-channelObj.SNR/10);
                    %                 hScalQuant.BoundaryPoints = wifi_params.modulation(log2(MCS.M)).bound_pts_wo_noiseVar/NoiseVariance;
                    %
                    %                 quantized_demodulatorOut_LLR_Value = step(hScalQuant, -decoderIn);
                    %
                    %                 decoderOut = step(hDec_SD, double(quantized_demodulatorOut_LLR_Value));
                    %                 decoderOut = step(hDec_SD, -decoderIn);
                    decoderOut(:,i_amountOfStreams) = step(hDec_SD, decoderIn);
                    demodulatorOut = demodulatorOut_hard;
            end

        case 'LDPC'
            %% LDPC %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            hDec = comm.LDPCDecoder(wifi_params.coding.UsedLDPC_matrix,'IterationTerminationCondition','Parity check satisfied');
            LDPC_N = size(wifi_params.coding.UsedLDPC_matrix,2);
            LDPC_K = LDPC_N - size(wifi_params.coding.UsedLDPC_matrix,1);
            % load data from demodulator
            parsed_encoded_PSDU_tx = decoderIn;
            % inverse parse operation
            PSDU_rx_plus_service_cw = cell(wifi_params.coding.N_cw,1);
            receivedEncodedBits = cell(wifi_params.coding.N_cw,1);
            for i_cw = 1:wifi_params.coding.N_cw
                receivedEncodedBits{i_cw} = parsed_encoded_PSDU_tx(1:wifi_params.coding.shortening.n_bitwoshrt_cw(i_cw)+sum(wifi_params.coding.puncturing.parity_ind(i_cw,:))+wifi_params.coding.repeating.n_rep_cw(i_cw),1);
                if i_cw ~= wifi_params.coding.N_cw
                    parsed_encoded_PSDU_tx = parsed_encoded_PSDU_tx(wifi_params.coding.shortening.n_bitwoshrt_cw(i_cw)+sum(wifi_params.coding.puncturing.parity_ind(i_cw,:))+wifi_params.coding.repeating.n_rep_cw(i_cw)+1:end,1);
                end
            end

            dataOut = cell(wifi_params.coding.N_cw,1);
            decoderIn_NoRepeated = cell(wifi_params.coding.N_cw,1); % #1
            % decoding process
            for i_cw = 1:wifi_params.coding.N_cw

                actual_received_codeword = receivedEncodedBits{i_cw};
                % discard repeating bits
                if wifi_params.coding.repeating.N_rep ~= 0
                    if wifi_params.coding.puncturing.N_punc ~= 0
                        %                     disp(['NOTE: In this case repetition should not be performed'])
                    else
                        actual_received_codeword = actual_received_codeword(1:end-wifi_params.coding.repeating.n_rep_cw(i_cw));
                    end
                end

                received_shortenedBits = actual_received_codeword(1:wifi_params.coding.shortening.n_bitwoshrt_cw(i_cw));
                received_parityBits = actual_received_codeword(wifi_params.coding.shortening.n_bitwoshrt_cw(i_cw)+1:end);
                if length(received_parityBits) ~= sum(wifi_params.coding.puncturing.parity_ind(i_cw,:))
                    error('Nonequal number of parity bits -> LDPC Decoder')
                end

                decoderIn_NoRepeated{i_cw} = [received_shortenedBits; received_parityBits];

                % mux decoder input
                rx_encodedDataPlusParity = [received_shortenedBits; zeros(wifi_params.coding.shortening.n_shrt_cw(i_cw),1); received_parityBits; zeros(wifi_params.coding.puncturing.n_punc_cw(i_cw),1)];
                if strcmp(wifi_params.coding.decision_type,'HD')
                    rx_encodedDataPlusParity = (-2)*rx_encodedDataPlusParity+1;
                end

                dataOut_tmp = hDec.step(rx_encodedDataPlusParity);

                dataOut{i_cw} = dataOut_tmp;

                PSDU_rx_plus_service_cw{i_cw} = dataOut_tmp(1:wifi_params.coding.shortening.n_bitwoshrt_cw(i_cw));
            end

            if length(vertcat(PSDU_rx_plus_service_cw{:})) ~= wifi_params.coding.N_pld
                error('Error in decoding operation --> LDPC Decoder')
            end

            PSDU_rx_plus_service = vertcat(PSDU_rx_plus_service_cw{:});
            PSDU_rx = PSDU_rx_plus_service(service_length+1:end);

            % decoder output with shortening bits
            %         dataOut = vertcat(dataOut{:});
            decoderIn = vertcat(decoderIn_NoRepeated{:});
            decoderOut = PSDU_rx_plus_service;
            %
            demodulatorOut = demodulatorOut_hard;
    end
end
%% BCC ENCODER DEPARSING
inverseParsingIn = decoderOut;
inverseParsingTransfer = reshape(inverseParsingIn.',1,size(inverseParsingIn,1)*size(inverseParsingIn,2));
inverseParsingOut = inverseParsingTransfer.';
%inverseParsingOut = inverseParsingIn;


%% EXTRACT PSDU INFORMATION
% PSDU
switch wifi_params.coding.type
    case 'BCC'
        descramblerIn = circshift(inverseParsingOut,-DEC_offset*wifi_params.MCS(i_mcs).N_es);
        
        scr_seed = wifi_params.scrambling.scr_seed;
        scramblingSeq = BCC_ScramblingSeqGen(descramblerIn, scr_seed);
        descramblerOut = xor(descramblerIn, scramblingSeq);
        
        % tail of encoderIn must be zero (6 or 12 bits)
        %         descramblerOut(end-DEC_offset+1:end) = zeros(DEC_offset,1);
        
        PSDU_rx_plus_service = descramblerOut(1:end-wifi_params.coding.N_pad_bits-DEC_offset);
        PSDU_rx = PSDU_rx_plus_service(service_length+1:end);
        
        
        for i_amountOfStreams = 1:wifi_params.MCS(i_mcs).N_es
        % change for comparison encoder input vs decoder output
        compareIn(:,i_amountOfStreams) = decoderOut(DEC_offset+1:end,i_amountOfStreams); % [SERVICE, DATA, PAD_BITS] % equal to desramblerIn
        end
        
    case 'LDPC'
        
    otherwise
        error('Wrong channel coding type');
end

%% Save results

rxObj.output.PSDU = PSDU_rx; % user traffic data without overhead
rxObj.output.PSDU_plus_service = PSDU_rx_plus_service; % user traffic data + SERVICE zeros field but without overhead

rxObj.output.coded_data = compareIn; % decoder output - user traffic data + SERVICE zeros field with overhead
rxObj.output.uncoded_data = demodulatorOut; % decoder input (raw data) (demodulated bits)
rxObj.output.descramblerIn = logical(descramblerIn);
rxObj.output.chan = y_rx;

end