middlepad.py 8.03 KB
Newer Older
Denis Arrivault's avatar
Denis Arrivault committed
1
# -*- coding: utf-8 -*-
Denis Arrivault's avatar
Denis Arrivault committed
2
# ######### COPYRIGHT #########
3
4
# Credits
# #######
Denis Arrivault's avatar
Denis Arrivault committed
5
#
6
7
# Copyright(c) 2015-2018
# ----------------------
Denis Arrivault's avatar
Denis Arrivault committed
8
#
9
10
11
12
13
# * `LabEx Archimède <http://labex-archimede.univ-amu.fr/>`_
# * `Laboratoire d'Informatique Fondamentale <http://www.lif.univ-mrs.fr/>`_
#   (now `Laboratoire d'Informatique et Systèmes <http://www.lis-lab.fr/>`_)
# * `Institut de Mathématiques de Marseille <http://www.i2m.univ-amu.fr/>`_
# * `Université d'Aix-Marseille <http://www.univ-amu.fr/>`_
Denis Arrivault's avatar
Denis Arrivault committed
14
15
#
# This software is a port from LTFAT 2.1.0 :
16
# Copyright (C) 2005-2018 Peter L. Soendergaard <peter@sonderport.dk>.
Denis Arrivault's avatar
Denis Arrivault committed
17
18
19
20
#
# Contributors
# ------------
#
21
22
# * Denis Arrivault <contact.dev_AT_lis-lab.fr>
# * Florent Jaillet <contact.dev_AT_lis-lab.fr>
Denis Arrivault's avatar
Denis Arrivault committed
23
24
25
26
#
# Description
# -----------
#
27
28
29
30
# ltfatpy is a partial Python port of the
# `Large Time/Frequency Analysis Toolbox <http://ltfat.sourceforge.net/>`_,
# a MATLAB®/Octave toolbox for working with time-frequency analysis and
# synthesis.
Denis Arrivault's avatar
Denis Arrivault committed
31
32
33
34
#
# Version
# -------
#
35
# * ltfatpy version = 1.0.14
Denis Arrivault's avatar
Denis Arrivault committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# * LTFAT version = 2.1.0
#
# Licence
# -------
#
# 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 <http://www.gnu.org/licenses/>.
#
# ######### COPYRIGHT #########
Denis Arrivault's avatar
Denis Arrivault committed
55
56


Denis Arrivault's avatar
Denis Arrivault committed
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""Module of symmetrical zero-extension or cut of data

Ported from ltfat_2.1.0/fourier/middlepad.m

.. moduleauthor:: Florent Jaillet
"""

from __future__ import print_function, division

import six
import numpy as np

from ltfatpy.comp.assert_sigreshape_pre import assert_sigreshape_pre
from ltfatpy.comp.assert_sigreshape_post import assert_sigreshape_post


def middlepad(f, L, dim=None, centering='wp'):
    """Symmetrically zero-extends or cuts a function

    - Usage:

        | ``h = middlepad(f, L)``
        | ``h = middlepad(f, L, dim)``
        | ``h = middlepad(f, L, ...)``

    - Input parameters:

    :param numpy.ndarray f: Input array
    :param int L: Length of the output array
    :param int dim: Axis over which to zero-extend or cut **f**
    :param str centering: Flag specifying if **f** is whole point even when
        ``centering='wp'`` or half point even when ``centering='hp'``

    - Output parameters:

    :returns: Zero-extended or cut array
    :rtype: numpy.ndarray

    ``middlepad(f, L)`` zero-extends or cuts **f** to length **L** by
    inserting zeros in the middle of the vector, or by cutting in the middle
    of the vector.

    If **f** is whole-point even, ``middlepad(f, L)`` will also be
    whole-point even.

    ``middlepad(f, L, dim)`` does the same along dimension **dim**.

    If **f** has even length, then **f** will not be purely zero-extended,
    but the last element will be repeated once and multiplied by ``1/2``.
    That is, the support of **f** will increase by one!

    Adding the flag ``centering='wp'`` will cut or extend whole point even
    functions (the default). Adding ``centering='hp'`` will do the same for
    half point even functions.

    .. seealso:: :func:`~ltfatpy.fourier.isevenfunction.isevenfunction`,
                 :func:`~ltfatpy.sigproc.fir2long.fir2long`,
                 :func:`~ltfatpy.fourier.fftresample.fftresample`
    """

    # Note: For a future improvement, it might be possible to replace the use
    # of numpy.concatenate with numpy.r_ in this function to simplify the code.

    if not isinstance(L, six.integer_types):
        raise TypeError('L must be an integer.')

    if L < 1:
        raise ValueError('L must be larger than 0.')

    f, L, Ls, W, dim, permutedsize, order = assert_sigreshape_pre(f, L, dim)

    Lorig = Ls

    # Skip the main section if there is nothing to do. This is necessary
    # because some of the code below cannot handle the case of 'nothing to do'
    if L != Ls:
        if centering == 'wp':

            # ---------------   WPE case --------------------------------------
            if Lorig == 1:
                # Rather trivial case
                h = np.concatenate((f[np.newaxis, 0, :],
                                    np.zeros((L-1, W), dtype=f.dtype)))

            else:

                if Lorig > L:
                    # Cut

                    if L % 2 == 0:

                        # L even. Use average of endpoints.
149
150
151
152
                        h = np.concatenate((f[:L//2, :],
                                            (f[np.newaxis, L//2, :] +
                                             f[np.newaxis, Lorig-L//2, :]) / 2,
                                            f[Lorig-L//2+1:Lorig, :]))
Denis Arrivault's avatar
Denis Arrivault committed
153
154
155
156

                    else:

                        # No problem, just cut.
157
158
                        h = np.concatenate((f[:(L+1)//2, :],
                                            f[Lorig-(L-1)//2:Lorig, :]))
Denis Arrivault's avatar
Denis Arrivault committed
159
160
161
162
163
164
165

                else:
                    d = L - Lorig

                    # Extend
                    if Lorig % 2 == 0:
                        # Lorig even. We must split a value.
166
167
                        h = np.concatenate((f[:Lorig//2, :],
                                            f[np.newaxis, Lorig//2, :]/2,
Denis Arrivault's avatar
Denis Arrivault committed
168
                                            np.zeros((d-1, W), dtype=f.dtype),
169
170
                                            f[np.newaxis, Lorig//2, :]/2,
                                            f[Lorig//2+1:Lorig, :]))
Denis Arrivault's avatar
Denis Arrivault committed
171
172
173

                    else:
                        # Lorig is odd, we can just insert zeros.
174
                        h = np.concatenate((f[:(Lorig+1)//2, :],
Denis Arrivault's avatar
Denis Arrivault committed
175
                                            np.zeros((d, W), dtype=f.dtype),
176
                                            f[(Lorig+1)//2:Lorig, :]))
Denis Arrivault's avatar
Denis Arrivault committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

        elif centering == 'hp':

            # ------------------ HPE case ------------------------------------

            # NOTE: There is a bug here in LTFAT 2.1.0 for Octave, see:
            # https://sourceforge.net/p/ltfat/bugs/123
            # This bug arise because the case "if Lorig==1" hasn't been
            # implemented in the Octave code when centering = 'hp'.
            # To solve this bug, we simply remove the test "if Lorig==1", which
            # seems to lead to satisfactory results.

            if Lorig > L:

                d = Lorig-L
                # Cut

                if L % 2 == 0:
                    # L even

                    # No problem, just cut.
198
                    h = np.concatenate((f[:L//2, :], f[Lorig-L//2:Lorig, :]))
Denis Arrivault's avatar
Denis Arrivault committed
199
200
201

                else:
                    # Average of endpoints.
202
203
204
205
                    h = np.concatenate((f[:(L-1)//2, :],
                                        (f[np.newaxis, (L-1)//2, :] +
                                         f[np.newaxis, Lorig-(L+1)//2, :]) / 2,
                                        f[Lorig-(L-1)//2:Lorig, :]))
Denis Arrivault's avatar
Denis Arrivault committed
206
207
208
209
210
211
212
213
214

            else:

                d = L-Lorig

                # Extend
                if Lorig % 2 == 0:

                    # Lorig even. We can just insert zeros in the middle.
215
                    h = np.concatenate((f[:Lorig//2, :],
Denis Arrivault's avatar
Denis Arrivault committed
216
                                        np.zeros((d, W), dtype=f.dtype),
217
                                        f[Lorig//2:Lorig, :]))
Denis Arrivault's avatar
Denis Arrivault committed
218
219
220

                else:
                    # Lorig odd. We need to split a value in two
221
222
                    h = np.concatenate((f[:(Lorig-1)//2, :],
                                        f[np.newaxis, (Lorig-1)//2, :]/2,
Denis Arrivault's avatar
Denis Arrivault committed
223
                                        np.zeros((d-1, W), f.dtype),
224
225
                                        f[np.newaxis, (Lorig-1)//2, :]/2,
                                        f[(Lorig+1)//2:Lorig, :]))
Denis Arrivault's avatar
Denis Arrivault committed
226
227
228
229
230
231
232
233
234

    else:
        # we don't want this function to return a reference or a view to the
        # input, so we make a copy
        h = f.copy()

    h = assert_sigreshape_post(h, dim, permutedsize, order)

    return h