# Source code :: _extend

[Return]
 ``` 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``` ```## This file is part of mlpy. ## This code is written by Davide Albanese, . ## (C) 2009 Fondazione Bruno Kessler - Via Santa Croce 77, 38100 Trento, ITALY. ## This program 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 of the License, or ## (at your option) any later version. ## This program 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 program. If not, see . __all__ = ['extend'] import numpy as np def extend(x, method='reflection', length='powerof2'): """ Extend the 1D numpy array x beyond its original length. :Parameters: x : 1d ndarray data method : string ('reflection', 'periodic', 'zeros') indicates which extension method to use length : string ('powerof2', 'double') indicates how to determinate the length of the extended data :Returns: xe : 1d ndarray extended version of x Example: >>> import numpy as np >>> import mlpy >>> a = np.array([1,2,3,4,5]) >>> mlpy.extend(a, method='periodic', length='powerof2') array([1, 2, 3, 4, 5, 1, 2, 3]) """ if length == 'powerof2': lt = 2*2**int( np.log2(x.shape[0])) lp = lt - x.shape[0] elif length == 'double': lp = x.shape[0] else: ValueError("length %s is not available" % length) if method == 'reflection': xret = np.append(x, x[::-1][:lp]) elif method == 'periodic': xret = np.append(x, x[:lp]) elif method == 'zeros': xret = np.append(x, np.zeros(lp, dtype=x.dtype)) else: ValueError("method %s is not available" % method) return xret ```