{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The set of all possible windows with identical dual is defined by the set of linear constraints of Eq. "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "eq_STFT_AllDualWinsCond",
              "domain": "math",
              "role": "numref",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " in the "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "tutorial_stft",
              "domain": null,
              "role": "ref",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " section of the "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "user_guide",
              "domain": null,
              "role": "ref",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". There it is also derived that "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "ShortTimeFFT.dual_win == ShortTimeFFT.m_pts * ShortTimeFFT.win"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " needs to hold for an STFT to be a unitary mapping."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "A unitary mapping preserves the value of the scalar product, i.e.,"
            }
          ]
        },
        {
          "__type": "Math",
          "__tag": 4058,
          "value": "\\langle x, y\\rangle = \\sum_k x[k]\\, \\overline{y[k]}\n\\stackrel{\\stackrel{\\text{unitary}}{\\downarrow}}{=}\n\\sum_{q,p} S_x[q,p]\\, \\overline{S_y[q,p]}\n= \\langle S_x[q,p], S_y[q,p]\\rangle\\ ,"
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "with "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "S_{x,y}"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " being the STFT of "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "x,y"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". Hence, the energy "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "E_x=T\\sum_k |x[k]|^2"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " of a signal is also preserved. This is also illustrated in the example below."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Thie reason of distinguishing between no scaling (i.e., parameter "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "scale_to",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": null,
                "version": null,
                "kind": "local",
                "path": "scale_to"
              },
              "kind": "local"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "None"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ") and unitary scaling (i.e., "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "scale_to = 'unitary'"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ") is due to the utilized FFT function not being unitary (i.e., using the default value "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "'backward'"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " for the "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "fft",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "scipy",
                "version": "*",
                "kind": "api",
                "path": "scipy.fft._basic:fft"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " parameter "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "norm",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ")."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Yields": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Methods": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Returns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Create instance where the window and its dual are equal up to a scaling factor."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Receives": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warnings": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Attributes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "desired_win",
              "annotation": "np.ndarray",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "A real-valued or complex-valued 1d array containing the sample of the desired window."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "hop",
              "annotation": "int",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The increment in samples, by which the window is shifted in each step."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "fs",
              "annotation": "float",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Sampling frequency of input signal and window. Its relation to the sampling interval "
                    },
                    {
                      "__type": "InlineRole",
                      "__tag": 4003,
                      "value": "T",
                      "domain": null,
                      "role": null,
                      "inventory": null
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "T = 1 / fs"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "fft_mode",
              "annotation": "'twosided', 'centered', 'onesided', 'onesided2X'",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Mode of FFT to be used (default 'onesided'). See property "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "fft_mode",
                      "reference": {
                        "__type": "RefInfo",
                        "__tag": 4000,
                        "module": null,
                        "version": null,
                        "kind": "local",
                        "path": "fft_mode"
                      },
                      "kind": "local"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " for details."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "mfft: int | None",
              "annotation": "",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Length of the FFT used, if a zero padded FFT is desired. If "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "None"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " (default), the length of the window "
                    },
                    {
                      "__type": "InlineRole",
                      "__tag": 4003,
                      "value": "win",
                      "domain": null,
                      "role": null,
                      "inventory": null
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is used."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "scale_to",
              "annotation": "'magnitude' | 'psd' | 'unitary' | None",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If not "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "None"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " (default) the window function is scaled, so each STFT column represents either a 'magnitude' or a power spectral density ('psd') spectrum, Alternatively, the STFT can be scaled to a`unitary` mapping, i.e., dividing the window by "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "np.sqrt(mfft)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " and multiplying the dual window by the same amount."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "phase_shift",
              "annotation": "int | None",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If set, add a linear phase "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "phase_shift",
                      "reference": {
                        "__type": "RefInfo",
                        "__tag": 4000,
                        "module": null,
                        "version": null,
                        "kind": "local",
                        "path": "phase_shift"
                      },
                      "kind": "local"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " / "
                    },
                    {
                      "__type": "InlineRole",
                      "__tag": 4003,
                      "value": "mfft",
                      "domain": null,
                      "role": null,
                      "inventory": null
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " * "
                    },
                    {
                      "__type": "InlineRole",
                      "__tag": 4003,
                      "value": "f",
                      "domain": null,
                      "role": null,
                      "inventory": null
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " to each frequency "
                    },
                    {
                      "__type": "InlineRole",
                      "__tag": 4003,
                      "value": "f",
                      "domain": null,
                      "role": null,
                      "inventory": null
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". The default value of 0 ensures that there is no phase shift on the zeroth slice (in which t=0 is centered). See property "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "phase_shift",
                      "reference": {
                        "__type": "RefInfo",
                        "__tag": 4000,
                        "module": null,
                        "version": null,
                        "kind": "local",
                        "path": "phase_shift"
                      },
                      "kind": "local"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " for more details."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "An instance is created were window and dual window are equal as well as being closest to the parameter "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "desired_win",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": null,
                "version": null,
                "kind": "local",
                "path": "desired_win"
              },
              "kind": "local"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " in the least-squares sense, i.e., minimizing "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "abs(win-desired_win)**2"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". Hence, "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "win",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " has the same length as "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "desired_win",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": null,
                "version": null,
                "kind": "local",
                "path": "desired_win"
              },
              "kind": "local"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". Then a scaling factor is applied accoring to the "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "scale_to",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": null,
                "version": null,
                "kind": "local",
                "path": "scale_to"
              },
              "kind": "local"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " parameter."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "All other parameters have the identical meaning as in the initializer."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "To be able to calculate a valid window, "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "desired_win",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": null,
                "version": null,
                "kind": "local",
                "path": "desired_win"
              },
              "kind": "local"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " needs to have a valid dual STFT window for the given "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "hop",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": null,
                "version": null,
                "kind": "local",
                "path": "hop"
              },
              "kind": "local"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " interval. If this is not the case, a "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "ValueError"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is raised."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Other Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    }
  },
  "_ordered_sections": [
    "Summary",
    "Extended Summary",
    "Parameters",
    "Attributes",
    "Methods",
    "Returns",
    "Yields",
    "Receives",
    "Other Parameters",
    "Raises",
    "Warns",
    "Warnings",
    "Notes"
  ],
  "item_file": "/scipy/signal/_short_time_fft.py",
  "item_line": 620,
  "item_type": "classmethod",
  "aliases": [
    "scipy.signal.ShortTimeFFT.from_win_equals_dual"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "The following example shows that an STFT can be indeed unitary:\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import matplotlib.pyplot as plt\nimport numpy as np\nfrom scipy.signal import ShortTimeFFT, windows\nm, hop, std = 36, 8, 5\ndesired_win = windows.gaussian(m, std, sym=True)\nSFT = ShortTimeFFT.from_win_equals_dual(desired_win, hop, fs=1/m,\n                                        fft_mode='twosided',\n                                        scale_to='unitary')\nnp.allclose(SFT.dual_win, SFT.win * SFT.m_num)  # check if STFT is unitary\nx1, x2 = np.tile([-1, -1, 1, 1], 5), np.tile([1, -1, -1, 1], 5)\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "np.sum(x1*x2) # scalar product is zero -> orthogonal signals\nnp.sum(x1**2)  # scalar product of x1 with itself\n",
        "execution_status": "failure"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "Sx11, Sx12 = SFT.spectrogram(x1), SFT.spectrogram(x1, x2)\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "np.sum(Sx12)  # STFT scalar product is also zero\nnp.sum(Sx11)  # == np.sum(x1**2)\n",
        "execution_status": "failure"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "fg1, (ax11, ax12) = plt.subplots(1, 2, tight_layout=True, figsize=(8, 4))\ns_fac = np.sqrt(SFT.mfft)\n_ = fg1.suptitle(f\"Scaled Unitary Window of {m} Sample Gaussian with \" +\n                 rf\"{hop=}, $\\sigma={std}$, Scale factor: {s_fac}\")\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "ax11.set(ylabel=\"Amplitude\", xlabel=\"Samples\", xlim=(0, m))\nax12.set(xlabel=\"Frequency Bins\", ylabel=\"Magnitude Spectrum\",\n         xlim=(0, 15), ylim=(1e-5, 1.5))\n",
        "execution_status": "failure"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "u_win_str = rf\"Unitary $\\times{s_fac:g}$\"\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "for x_, n_ in zip((desired_win, SFT.win*s_fac), ('Desired', u_win_str)):\n    ax11.plot(x_, '.-', alpha=0.5, label=n_)\n    X_ = np.fft.rfft(x_) / np.sum(abs(x_))\n    ax12.semilogy(abs(X_), '.-', alpha=0.5, label=n_)\nfor ax_ in (ax11, ax12):\n    ax_.grid(True)\n    ax_.legend()\n",
        "execution_status": "failure"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "plt.show()\n",
        "execution_status": "success"
      },
      {
        "__type": "Figure",
        "__tag": 4024,
        "value": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "scipy",
          "version": "1.17.1",
          "kind": "assets",
          "path": "fig-5cfc4177b810f5a9.png"
        }
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nNote that ``fftmode='twosided'`` is used, since we need sum over the complete\ntime frequency plane. Due to passing ``scale_to='unitary'`` the window\n``SFT.win`` is scaled by ``1/np.sqrt(SFT.mfft)``. Hence, ``SFT.win`` needs to\nbe scaled by `s_fac` in the plot above."
      }
    ],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "ShortTimeFFT",
        "reference": {
          "__type": "LocalRef",
          "__tag": 4022,
          "kind": "module",
          "path": "scipy.signal._short_time_fft:ShortTimeFFT"
        },
        "kind": "module"
      },
      "descriptions": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Class this property belongs to."
            }
          ]
        }
      ],
      "type": null
    },
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "ShortTimeFFT.spectrogram",
        "reference": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "current-module",
          "version": "current-version",
          "kind": "to-resolve",
          "path": "ShortTimeFFT.spectrogram"
        },
        "kind": "module"
      },
      "descriptions": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Calculate squared STFTs"
            }
          ]
        }
      ],
      "type": null
    },
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "closest_STFT_dual_window",
        "reference": {
          "__type": "LocalRef",
          "__tag": 4022,
          "kind": "module",
          "path": "scipy.signal._short_time_fft:closest_STFT_dual_window"
        },
        "kind": "module"
      },
      "descriptions": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Calculate the STFT dual window of a given window closest to a desired dual window."
            }
          ]
        }
      ],
      "type": null
    }
  ],
  "signature": null,
  "references": null,
  "qa": "scipy.signal._short_time_fft:ShortTimeFFT.from_win_equals_dual",
  "arbitrary": [],
  "local_refs": [
    "desired_win",
    "fft_mode",
    "fs",
    "hop",
    "mfft: int | None",
    "phase_shift",
    "scale_to"
  ]
}