aboutsummaryrefslogtreecommitdiffstats
path: root/src/gr-fadingui/python/multipath_fading.py
blob: 3c34a79ffbbd610da051d409c3d7eeae24a84fc8 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2021 Sara Cinzia Halter.
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#


import numpy as np
from numpy.fft import fft,ifft,fftshift
from gnuradio import gr

from fadingui.logger import get_logger
log = get_logger("multipath_fading")

class multipath_fading(gr.sync_block):
    """
    docstring for block multipath_fading
    """
    def __init__(self, amplitudes, delays, los):  # only default arguments here
        """arguments to this function show up as parameters in GRC"""
        gr.sync_block.__init__(
            self,
            name='Embedded Python Block',   # will show up in GRC
            in_sig=[np.complex64],
            out_sig=[np.complex64]
        )
        # if an attribute with the same name as a parameter is found,
        # a callback is registered (properties work, too).
        self.amplitudes = amplitudes
        self.delays = delays
        self.temp = [0]
        log.debug(los) #TO DO: True False unterscheidung 
        if los == True:
            self.los = 1
            log.debug("Los True")
        else: 
            self.los = 0
            log.debug("Los False")


        
        

    def work(self, input_items, output_items):
        """example: multiply with constant"""
        inp = input_items[0]
        oup = output_items[0]
        

        if len(self.amplitudes) != len(self.delays):    # Test: Es muss gleich viele Werte für Delays und Amplituden haben.
            raise Exception("Amplitudes and Delay length dont match")
        if np.min(self.delays)<0:   #Negativ Check
            raise Exception("Delay can't be negativ")

        
        #    raise Exception("Delay length can't be one")
        #if np.min(self.delays)<=1:
        #    raise Exception("Delay length can't be one")



        max_order = 2 * np.floor(np.max(self.delays)) + 1 #Max Werte herausfinden für länge 
        max_samples = np.arange(0, max_order +1) 
        max_len = len(max_samples) #Für Filter

        sum_x = np.zeros(int(max_len))

        for (a,d) in zip(self.amplitudes,self.delays):
            # if d-1 <= 0:
            #     x = np.concatenate([[a], np.zeros(max_len-1)])
            # else:    
            order = 2 * np.floor(d) + 1

            skip = np.floor(d) - (order - 1) / 2 #M sollte immer 0 sein 
            assert skip >= 0

            samples = np.arange(0, order +1)  

            h = a*(np.sinc(samples-d)) #sinc
            h_len = np.concatenate([h, np.zeros(max_len-len(h))])

            sum_x += h_len

            #x = np.concatenate([np.zeros(d-1), [a], np.zeros(max_len-d)])
            #sum_x += x
        
        sum_x[0] = self.los
        #log.debug(sum_x)


        y = np.convolve(inp, sum_x)
        
        # signal_shifted = np.convolve(h, inp, mode='full')
        # y = signal_shifted

        y+=np.concatenate([self.temp,np.zeros(len(y)-len(self.temp))])

        oup[:] = y[:len(inp)]
        self.temp = y[len(inp):]        
        

        return len(oup)