File: idgt.py

package info (click to toggle)
python-ltfatpy 1.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,412 kB
  • sloc: ansic: 8,546; python: 6,470; makefile: 15
file content (209 lines) | stat: -rw-r--r-- 6,859 bytes parent folder | download | duplicates (2)
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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# -*- coding: utf-8 -*-
# ######### COPYRIGHT #########
# Credits
# #######
#
# Copyright(c) 2015-2025
# ----------------------
#
# * `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/>`_
#
# This software is a port from LTFAT 2.1.0 :
# Copyright (C) 2005-2025 Peter L. Soendergaard <peter@sonderport.dk>.
#
# Contributors
# ------------
#
# * Denis Arrivault <contact.dev_AT_lis-lab.fr>
# * Florent Jaillet <contact.dev_AT_lis-lab.fr>
#
# Description
# -----------
#
# 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.
#
# Version
# -------
#
# * ltfatpy version = 1.1.2
# * 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 #########


"""Module of idgt calculation

Ported from ltfat_2.1.0/gabor/idgt.m

.. moduleauthor:: Denis Arrivault
"""

import numpy as np
from ltfatpy.gabor.dgtlength import dgtlength
from ltfatpy.gabor.gabwin import gabwin
from ltfatpy.comp.comp_isepdgt import comp_isepdgt
from ltfatpy.tools.postpad import postpad
from ltfatpy.comp.comp_sigreshape_post import comp_sigreshape_post


def idgt(coef, g, a, Ls=None, pt='freqinv'):
    r"""Inverse discrete Gabor transform

    - Usage:

        | ``(f, g) = idgt(c, g, a)``
        | ``(f, g) = idgt(c, g, a, Ls)``
        | ``(f, g) = idgt(c, g, a, Ls, pt)``

    - Input parameters:

    :param numpy.ndarray c: Array of coefficients
    :param g: Window function
    :param int a: Length of time shift
    :param int Ls: Length of signal
    :param str pt: 'freqinv' or 'timeinv'. Default is 'freqinv'.
    :type g: str, dict or numpy.ndarray

    - Output parameters:

    :return: Signal (dtype = complex128)
    :rtype: numpy.ndarray

    ``idgt(c, g, a)`` computes the Gabor expansion of the input coefficients
    **c** with respect to the window **g** and time shift **a**. The number of
    channels is deduced from the size of the coefficients **c**.

    ``idgt(c, g, a, Ls)`` does as above but cuts or extends **f** to length
    **Ls**.

    ``(f, g)=idgt(...)`` additionally outputs the window used in the
    transform. This is useful if the window was generated from a description
    in a string or cell array.

    For perfect reconstruction, the window used must be a dual window of the
    one used to generate the coefficients.

    The window **g** may be a vector of numerical values, a text string or a
    cell array. See the help of :func:`~ltfatpy.gabor.gabwin` for more details.

    If **g** is a row vector, then the output will also be a row vector. If
    **c** is 3-dimensional, then ``idgt`` will return a matrix consisting of
    one column vector for each of the TF-planes in **c**.

    Assume that ``f=idgt(c, g, a, L)`` for an array **c** of size
    :math:`M \\times N`. Then the following holds for :math:`k=0,\\ldots,L-1`:

    .. math::

        f(l+1) = \\sum_{n=0}^{N-1}\\sum_{m=0}^{M-1}c(m+1,n+1)e^{2\\pi iml/M}
            g(l-an+1)

    - Additional parameters:

        ``idgt`` takes the following keyword at the end of the line of input
        arguments:

        pt='freqinv'
            Compute a DGT using a frequency-invariant phase. This
            is the default convention described above.
        pt='timeinv'
            Compute a DGT using a time-invariant phase. This
            convention is typically used in FIR-filter algorithms.

    - Examples:

        The following example demonstrates the basic principles for getting
        perfect reconstruction (short version)::

            >>> from ltfatpy import greasy
            >>> from ltfatpy import dgt
            >>> f = greasy()[0]   # Input test signal
            >>> a = 32  # time shift
            >>> M = 64  # frequency shift
            >>> gs = {'name': 'blackman', 'M': 128}  # synthesis window
            >>> # analysis window
            >>> ga = {'name' : ('dual', gs['name']), 'M' : 128}
            >>> (c, Ls) = dgt(f, ga, a, M)[0:2]  # analysis
            >>> # ... do interesting stuff to c at this point ...
            >>> r = idgt(c, gs, a, Ls)[0]  # synthesis
            >>> np.linalg.norm(f-r) < 1e-10 # test
            True

    .. seealso:: :func:`~ltfatpy.gabor.dgt.dgt`,
        :func:`~ltfatpy.gabor.gabwin.gabwin`, :func:`dwilt`,
        :func:`~ltfatpy.gabor.gabtight.gabtight`
  """
    if (not isinstance(g, np.ndarray) and not isinstance(g, str) and
       not isinstance(g, dict)):
        raise TypeError('g must be a numpy.array or str or dict.')

    if (isinstance(g, np.ndarray) and g.size < 2):
        raise ValueError('g must be a vector (you probably forgot to supply' +
                         ' the window function as input parameter.)')

    # Define initial value for flags and key/value pairs.
    if coef.ndim < 2:
        raise ValueError('coef must have at least 2 dimensions')
    M = coef.shape[0]
    N = coef.shape[1]
    if coef.ndim > 2:
        W = coef.shape[2]
    else:
        W = 1

    if not isinstance(a, int):
        raise TypeError('a must be an integer')

    L = N * a
    Ltest = dgtlength(L, a, M)
    if Ltest != L:
        ValueError('Incorrect size of coefficient array or "a" parameter. ' +
                   ' See the help of DGTLENGTH for the requirements.')
    # verify pt
    if pt == 'timeinv':
        pt = 1
    elif pt == 'freqinv':
        pt = 0
    else:
        mes = "pt (" + str(pt) + ") argument should be 'timeinv' or 'freqinv'."
        raise ValueError(mes)

    # Determine the window
    gnum = gabwin(g, a, M, L)[0]
    f = comp_isepdgt(coef, gnum, a, pt)

    # Cut or extend f to the correct length, if desired.
    if Ls is not None:
        f = postpad(f, Ls)
    else:
        Ls = L

    f = comp_sigreshape_post(f, Ls, 0, (0, W))
    return (f, gnum)

if __name__ == '__main__':  # pragma: no cover
    import doctest
    doctest.testmod()