11
22# Copyright 2012 Veeresh Taranalli <[email protected] > 33#
4- # This file is part of CommPy.
4+ # This file is part of CommPy.
55#
66# CommPy is free software: you can redistribute it and/or modify
77# it under the terms of the GNU General Public License as published by
1616# You should have received a copy of the GNU General Public License
1717# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
19- """
19+ """
2020============================================
2121Pulse Shaping Filters (:mod:`commpy.filters`)
2222============================================
2323
2424.. autosummary::
2525 :toctree: generated/
26-
27- rcosfilter -- Class representing convolutional code trellis.
28- rrcosfilter -- Convolutional Encoder.
29- gaussianfilter -- Convolutional Decoder using the Viterbi algorithm.
26+
27+ rcosfilter -- Raised Cosine (RC) Filter.
28+ rrcosfilter -- Root Raised Cosine (RRC) Filter.
29+ gaussianfilter -- Gaussian Filter.
30+ rectfilter -- Rectangular Filter.
3031
3132"""
3233
3334import numpy as np
3435
35- __all__ = ['rcosfilter' , 'rrcosfilter' , 'gaussianfilter' ]
36+ __all__ = ['rcosfilter' , 'rrcosfilter' , 'gaussianfilter' , 'rectfilter' ]
3637
3738def rcosfilter (N , alpha , Ts , Fs ):
3839 """
3940 Generates a raised cosine (RC) filter (FIR) impulse response.
40-
41+
4142 Parameters
4243 ----------
43- N : int
44+ N : int
4445 Length of the filter in samples.
4546
46- alpha: float
47+ alpha : float
4748 Roll off factor (Valid values are [0, 1]).
4849
4950 Ts : float
5051 Symbol period in seconds.
5152
52- Fs : float
53+ Fs : float
5354 Sampling Rate in Hz.
5455
5556 Returns
@@ -58,63 +59,63 @@ def rcosfilter(N, alpha, Ts, Fs):
5859 h_rc : 1-D ndarray (float)
5960 Impulse response of the raised cosine filter.
6061
61- time_idx : 1-D ndarray (float)
62+ time_idx : 1-D ndarray (float)
6263 Array containing the time indices, in seconds, for the impulse response.
6364 """
6465
6566 T_delta = 1 / float (Fs )
6667 time_idx = ((np .arange (N )- N / 2 ))* T_delta
6768 sample_num = np .arange (N )
6869 h_rc = np .zeros (N , dtype = float )
69-
70+
7071 for x in sample_num :
7172 t = (x - N / 2 )* T_delta
7273 if t == 0.0 :
7374 h_rc [x ] = 1.0
7475 elif alpha != 0 and t == Ts / (2 * alpha ):
75- h_rc [x ] = (np .pi / 4 )* (np .sin (np .pi * t / Ts )/ (np .pi * t / Ts ))
76+ h_rc [x ] = (np .pi / 4 )* (np .sin (np .pi * t / Ts )/ (np .pi * t / Ts ))
7677 elif alpha != 0 and t == - Ts / (2 * alpha ):
7778 h_rc [x ] = (np .pi / 4 )* (np .sin (np .pi * t / Ts )/ (np .pi * t / Ts ))
7879 else :
7980 h_rc [x ] = (np .sin (np .pi * t / Ts )/ (np .pi * t / Ts ))* \
8081 (np .cos (np .pi * alpha * t / Ts )/ (1 - (((2 * alpha * t )/ Ts )* ((2 * alpha * t )/ Ts ))))
81-
82- return time_idx , h_rc
82+
83+ return time_idx , h_rc
8384
8485def rrcosfilter (N , alpha , Ts , Fs ):
8586 """
8687 Generates a root raised cosine (RRC) filter (FIR) impulse response.
87-
88+
8889 Parameters
8990 ----------
90- N : int
91+ N : int
9192 Length of the filter in samples.
92-
93- alpha: float
93+
94+ alpha : float
9495 Roll off factor (Valid values are [0, 1]).
95-
96+
9697 Ts : float
9798 Symbol period in seconds.
98-
99- Fs : float
99+
100+ Fs : float
100101 Sampling Rate in Hz.
101-
102+
102103 Returns
103104 ---------
104105
105106 h_rrc : 1-D ndarray of floats
106107 Impulse response of the root raised cosine filter.
107-
108- time_idx : 1-D ndarray of floats
109- Array containing the time indices, in seconds, for
108+
109+ time_idx : 1-D ndarray of floats
110+ Array containing the time indices, in seconds, for
110111 the impulse response.
111112 """
112113
113114 T_delta = 1 / float (Fs )
114115 time_idx = ((np .arange (N )- N / 2 ))* T_delta
115116 sample_num = np .arange (N )
116117 h_rrc = np .zeros (N , dtype = float )
117-
118+
118119 for x in sample_num :
119120 t = (x - N / 2 )* T_delta
120121 if t == 0.0 :
@@ -129,48 +130,72 @@ def rrcosfilter(N, alpha, Ts, Fs):
129130 h_rrc [x ] = (np .sin (np .pi * t * (1 - alpha )/ Ts ) + \
130131 4 * alpha * (t / Ts )* np .cos (np .pi * t * (1 + alpha )/ Ts ))/ \
131132 (np .pi * t * (1 - (4 * alpha * t / Ts )* (4 * alpha * t / Ts ))/ Ts )
132-
133+
133134 return time_idx , h_rrc
134135
135136def gaussianfilter (N , alpha , Ts , Fs ):
136137 """
137138 Generates a gaussian filter (FIR) impulse response.
138-
139+
139140 Parameters
140141 ----------
141142
142- N : int
143+ N : int
143144 Length of the filter in samples.
144145
145- alpha: float
146+ alpha : float
146147 Roll off factor (Valid values are [0, 1]).
147-
148+
148149 Ts : float
149150 Symbol period in seconds.
150-
151- Fs : float
151+
152+ Fs : float
152153 Sampling Rate in Hz.
153-
154+
154155 Returns
155156 -------
156157
157158 h_gaussian : 1-D ndarray of floats
158159 Impulse response of the gaussian filter.
159-
160- time_index : 1-D ndarray of floats
160+
161+ time_index : 1-D ndarray of floats
161162 Array containing the time indices for the impulse response.
162163 """
163-
164+
164165 T_delta = 1 / float (Fs )
165166 time_idx = ((np .arange (N )- N / 2 ))* T_delta
166167 h_gaussian = (np .sqrt (np .pi )/ alpha )* np .exp (- ((np .pi * time_index / alpha )* (np .pi * time_index / alpha )))
167-
168- return time_idx , h_gaussian
169-
168+
169+ return time_idx , h_gaussian
170+
170171def rectfilter (N , Ts , Fs ):
171-
172+ """
173+ Generates a rectangular filter (FIR) impulse response.
174+
175+ Parameters
176+ ----------
177+
178+ N : int
179+ Length of the filter in samples.
180+
181+ Ts : float
182+ Symbol period in seconds.
183+
184+ Fs : float
185+ Sampling Rate in Hz.
186+
187+ Returns
188+ -------
189+
190+ h_rect : 1-D ndarray of floats
191+ Impulse response of the rectangular filter.
192+
193+ time_index : 1-D ndarray of floats
194+ Array containing the time indices for the impulse response.
195+ """
196+
172197 h_rect = np .ones (N )
173198 T_delta = 1 / float (Fs )
174199 time_idx = ((np .arange (N )- N / 2 ))* T_delta
175-
176- return time_idx , h_rect
200+
201+ return time_idx , h_rect
0 commit comments