{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "This function uses the more efficient one-sided FFT, i.e. "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "rfft",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "scipy",
                "version": "?",
                "kind": "module",
                "path": "scipy.fft._basic:rfft"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " / "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "irfft",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "scipy",
                "version": "?",
                "kind": "module",
                "path": "scipy.fft._basic:irfft"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", if "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "x"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is real-valued and in the time domain. Else, the two-sided FFT, i.e., "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "fft",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "scipy",
                "version": "?",
                "kind": "module",
                "path": "scipy.fft._basic:fft"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " / "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "ifft",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "scipy",
                "version": "?",
                "kind": "module",
                "path": "scipy.fft._basic:ifft"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", is used (all FFT functions are taken from the "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "scipy.fft",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "module",
                "path": "scipy.fft"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " module)."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "If a "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "window"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is applied to a real-valued "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "x"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", the one-sided spectral windowing function is determined by taking the average of the negative and the positive frequency component. This ensures that real-valued signals and complex signals with zero imaginary part are treated identically. I.e., passing "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "x"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " or passing "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "x.astype(np.complex128)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " produce the same numeric result."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "If the number of input or output samples is prime or has few prime factors, this function may be slow due to utilizing FFTs. Consult "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "prev_fast_len",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "scipy",
                "version": "?",
                "kind": "module",
                "path": "scipy.fft._helper:prev_fast_len"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "next_fast_len",
              "reference": {
                "__type": "RefInfo",
                "__tag": 4000,
                "module": "scipy",
                "version": "?",
                "kind": "module",
                "path": "scipy.fft._helper:next_fast_len"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " for determining efficient signal lengths. Alternatively, utilizing "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "resample_poly",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "module",
                "path": "scipy.signal._signaltools:resample_poly"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to calculate an intermediate signal (as illustrated in the example below) can result in significant speed increases."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "resample",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "module",
                "path": "scipy.signal._signaltools:resample"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is intended to be used for periodic signals with equidistant sampling intervals. For non-periodic signals, "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "resample_poly",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "module",
                "path": "scipy.signal._signaltools:resample_poly"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " may be a better choice. Consult the "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "scipy.interpolate",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "module",
                "path": "scipy.interpolate"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " module for methods of resampling signals with non-constant sampling intervals."
            }
          ]
        }
      ],
      "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": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "x_r",
              "annotation": "ndarray",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The resampled signal made up of "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "num"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " samples and sampling interval "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "T * n_x / num"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "t_r",
              "annotation": "ndarray, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "num"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " equidistant timestamps of "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "x_r",
                      "reference": {
                        "__type": "RefInfo",
                        "__tag": 4000,
                        "module": null,
                        "version": null,
                        "kind": "local",
                        "path": "x_r"
                      },
                      "kind": "local"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". This is only returned if parameter "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "t"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is not "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "None"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Resample "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "x"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "num"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " samples using the Fourier method along the given "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "axis"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        }
      ],
      "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": "x",
              "annotation": "array_like",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The input signal made up of equidistant samples. If "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "x"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is a multidimensional array, the parameter "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "axis"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " specifies the time/frequency axis. It is assumed here that "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "n_x = x.shape[axis]"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " specifies the number of samples and "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "T"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " the sampling interval."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "num",
              "annotation": "int",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The number of samples of the resampled output signal. It may be larger or smaller than "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "n_x"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "t",
              "annotation": "array_like, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "t"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is not "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "None"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", then the timestamps of the resampled signal are also returned. "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "t"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " must contain at least the first two timestamps of the input signal "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "x"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " (all others are ignored). The timestamps of the output signal are determined by "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "t[0] + T * n_x / num * np.arange(num)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " with "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "T = t[1] - t[0]"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". Default is "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "None"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "axis",
              "annotation": "int, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The time/frequency axis of "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "x"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " along which the resampling takes place. The default is 0."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "window",
              "annotation": "array_like, callable, str, float, or tuple, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If not "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "None"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", it specifies a filter in the Fourier domain, which is applied before resampling. I.e., the FFT "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "X"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " of "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "x"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is calculated by "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "X = W * fft(x, axis=axis)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "W"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " may be interpreted as a spectral windowing function "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "W(f_X)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " which consumes the frequencies "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "f_X = fftfreq(n_x, T)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                },
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "window"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is a 1d array of length "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "n_x"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " then "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "W=window"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". If "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "window"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is a callable then "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "W = window(f_X)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". Otherwise, "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "window"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is passed to "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "get_window",
                      "reference": {
                        "__type": "RefInfo",
                        "__tag": 4000,
                        "module": "scipy",
                        "version": "?",
                        "kind": "module",
                        "path": "scipy.signal.windows._windows:get_window"
                      },
                      "kind": "module"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", i.e., "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "W = fftshift(signal.get_window(window, n_x))"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". Default is "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "None"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "domain",
              "annotation": "'time' | 'freq', optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If set to "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "'time'"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " (default) then an FFT is applied to "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "x"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", otherwise ("
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "'freq'"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ") it is assumed that an FFT was already applied, i.e., "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "x = fft(x_t, axis=axis)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " with "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "x_t"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " being the input signal in the time domain."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The resampling is performed by shortening or zero-padding the FFT of "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "x"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". This has the advantages of providing an ideal antialiasing filter and allowing arbitrary up- or down-sampling ratios. The main drawback is the requirement of assuming "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "x"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " to be a periodic signal."
            }
          ]
        }
      ],
      "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": "build-install/usr/lib/python3.14/site-packages/scipy/signal/_signaltools.py",
  "item_line": 3695,
  "item_type": "function",
  "aliases": [
    "scipy.signal.resample"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "The following example depicts a signal being up-sampled from 20 samples to 100\nsamples. The ringing at the beginning of the up-sampled signal is due to\ninterpreting the signal being periodic. The red square in the plot illustrates that\nperiodictiy by showing the first sample of the next cycle of the signal.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.signal import resample\nn0, n1 = 20, 100  # number of samples\nt0 = np.linspace(0, 10, n0, endpoint=False)  # input time stamps\nx0 = np.cos(-t0**2/6)  # input signal\nx1 = resample(x0, n1)  # resampled signal\nt1 = np.linspace(0, 10, n1, endpoint=False)  # timestamps of x1\nfig0, ax0 = plt.subplots(1, 1, tight_layout=True)\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "ax0.set_title(f\"Resampling $x(t)$ from {n0} samples to {n1} samples\")\nax0.set(xlabel=\"Time $t$\", ylabel=\"Amplitude $x(t)$\")\nax0.plot(t1, x1, '.-', alpha=.5, label=f\"Resampled\")\nax0.plot(t0, x0, 'o-', alpha=.5, label=\"Original\")\nax0.plot(10, x0[0], 'rs', alpha=.5, label=\"Next Cycle\")\nax0.legend(loc='best')\n",
        "execution_status": "failure",
        "out": "<matplotlib.legend.Legend object at 0x7f3f687c6f90>\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "ax0.grid(True)\nplt.show()\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Figure",
        "__tag": 4024,
        "value": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "scipy",
          "version": "2.0.0.dev0+git20260907.41eeb59",
          "kind": "assets",
          "path": "fig-8ff92d9f5217e292.png"
        }
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe following example compares this function with a naive `~scipy.fft.rfft` /\n`~scipy.fft.irfft` combination: An input signal with a sampling interval of one\nsecond is upsampled by a factor of eight. The first figure depicts an odd number of\ninput samples whereas the second figure an even number. The upper subplots show the\nsignals over time: The input samples are marked by large green dots, the upsampled\nsignals by a continuous and a dashed line. The lower subplots show the magnitude\nspectrum: The FFT values of the input are depicted by large green dots, which lie\nin the frequency interval [-0.5, 0.5] Hz, whereas the frequency interval of the\nupsampled signal is [-4, 4] Hz. The continuous green line depicts the upsampled\nspectrum without antialiasing filter, which is a periodic continuation of the input\nspectrum. The blue x's and orange dots depict the FFT values of the signal created\nby the naive approach as well as this function's result.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import matplotlib.pyplot as plt\nimport numpy as np\nfrom scipy.fft import fftshift, fftfreq, fft, rfft, irfft\nfrom scipy.signal import resample, resample_poly\nfac, T0, T1 = 8, 1, 1/8  # upsampling factor and sampling intervals\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "for n0 in (15, 16):  # number of samples of input signal\n    n1 = fac * n0  # number of samples of upsampled signal\n    t0, t1 = T0 * np.arange(n0), T1 * np.arange(n1)  # time stamps\n    x0 = np.zeros(n0)  # input signal has two non-zero sample values\n    x0[n0//2], x0[n0//2+1] = n0 // 2, -(n0 // 2)\n\n    x1n = irfft(rfft(x0), n=n1) * n1 / n0  # naive resampling\n    x1r = resample(x0, n1)  # resample signal\n\n    # Determine magnitude spectrum:\n    x0_up = np.zeros_like(x1r)  # upsampling without antialiasing filter\n    x0_up[::n1 // n0] = x0\n    X0, X0_up = (fftshift(fft(x_)) / n0 for x_ in (x0, x0_up))\n    XX1 = (fftshift(fft(x_)) / n1 for x_ in (x1n, x1r))\n    f0, f1 = fftshift(fftfreq(n0, T0)), fftshift(fftfreq(n1, T1))  # frequencies\n    df = f0[1] - f0[0]  # frequency resolution\n\n    fig, (ax0, ax1) = plt.subplots(2, 1, layout='constrained', figsize=(5, 4))\n    ax0.set_title(rf\"Upsampling ${fac}\\times$ from {n0} to {n1} samples\")\n    ax0.set(xlabel=\"Time $t$ in seconds\", ylabel=\"Amplitude $x(t)$\",\n            xlim=(0, n1*T1))\n    ax0.step(t0, x0, 'C2o-', where='post', alpha=.3, linewidth=2,\n             label=\"$x_0(t)$ / $X_0(f)$\")\n    for x_, l_ in zip((x1n, x1r), ('C0--', 'C1-')):\n        ax0.plot(t1, x_, l_, alpha=.5, label=None)\n    ax0.grid()\n    ax1.set(xlabel=rf\"Frequency $f$ in hertz ($\\Delta f = {df*1e3:.1f}\\,$mHz)\",\n            ylabel=\"Magnitude $|X(f)|$\", xlim=(-0.7, 0.7))\n    ax1.axvspan(0.5/T0, f1[-1], color='gray', alpha=.2)\n    ax1.axvspan(f1[0], -0.5/T0, color='gray', alpha=.2)\n    ax1.plot(f1, abs(X0_up), 'C2-', f0, abs(X0),  'C2o', alpha=.3, linewidth=2)\n    for X_, n_, l_ in zip(XX1, (\"naive\", \"resample\"), ('C0x--', 'C1.-')):\n        ax1.plot(f1, abs(X_), l_, alpha=.5, label=n_)\n    ax1.grid()\n    fig.legend(loc='outside lower center', ncols=4)\n",
        "execution_status": "failure",
        "out": "Text(0.5, 1.0, 'Upsampling $8\\\\times$ from 15 to 120 samples')\n[Text(0.5, 0, 'Time $t$ in seconds'), Text(0, 0.5, 'Amplitude $x(t)$'), (0.0, 15.0)]\n[<matplotlib.lines.Line2D object at 0x7f3f68848ad0>]\n[<matplotlib.lines.Line2D object at 0x7f3f68848980>]\n[<matplotlib.lines.Line2D object at 0x7f3f68849010>]\n[Text(0.5, 0, 'Frequency $f$ in hertz ($\\\\Delta f = 66.7\\\\,$mHz)'), Text(0, 0.5, 'Magnitude $|X(f)|$'), (-0.7, 0.7)]\n<matplotlib.patches.Rectangle object at 0x7f3f684c1a90>\n<matplotlib.patches.Rectangle object at 0x7f3f684c1d10>\n[<matplotlib.lines.Line2D object at 0x7f3f68848ec0>, <matplotlib.lines.Line2D object at 0x7f3f68848d70>]\n[<matplotlib.lines.Line2D object at 0x7f3f68849400>]\n[<matplotlib.lines.Line2D object at 0x7f3f688492b0>]\n<matplotlib.legend.Legend object at 0x7f3f688486e0>\nText(0.5, 1.0, 'Upsampling $8\\\\times$ from 16 to 128 samples')\n[Text(0.5, 0, 'Time $t$ in seconds'), Text(0, 0.5, 'Amplitude $x(t)$'), (0.0, 16.0)]\n[<matplotlib.lines.Line2D object at 0x7f3f6884bb60>]\n[<matplotlib.lines.Line2D object at 0x7f3f6884ba10>]\n[<matplotlib.lines.Line2D object at 0x7f3f6884b8c0>]\n[Text(0.5, 0, 'Frequency $f$ in hertz ($\\\\Delta f = 62.5\\\\,$mHz)'), Text(0, 0.5, 'Magnitude $|X(f)|$'), (-0.7, 0.7)]\n<matplotlib.patches.Rectangle object at 0x7f3f68fd79d0>\n<matplotlib.patches.Rectangle object at 0x7f3f68fd74d0>\n[<matplotlib.lines.Line2D object at 0x7f3f68e44980>, <matplotlib.lines.Line2D object at 0x7f3f68c5acf0>]\n[<matplotlib.lines.Line2D object at 0x7f3f68c5ae40>]\n[<matplotlib.lines.Line2D object at 0x7f3f68c5af90>]\n<matplotlib.legend.Legend object at 0x7f3f6884b620>\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "plt.show()\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Figure",
        "__tag": 4024,
        "value": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "scipy",
          "version": "2.0.0.dev0+git20260907.41eeb59",
          "kind": "assets",
          "path": "fig-2e28aec570209992.png"
        }
      },
      {
        "__type": "Figure",
        "__tag": 4024,
        "value": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "scipy",
          "version": "2.0.0.dev0+git20260907.41eeb59",
          "kind": "assets",
          "path": "fig-ec39cdcc997c22bf.png"
        }
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe first figure shows that upsampling an odd number of samples produces identical\nresults. The second figure illustrates that the signal produced with the naive\napproach (dashed blue line) from an even number of samples does not touch all\noriginal samples. This deviation is due to `resample` correctly treating unpaired\nfrequency bins. I.e., the input `x1` has a bin pair ±0.5 Hz, whereas the output has\nonly one unpaired bin at -0.5 Hz, which demands rescaling of that bin pair.\nGenerally, special treatment is required if ``n_x != num`` and ``min(n_x, num)`` is\neven. If the bin values at `±m` are zero, obviously, no special treatment is\nneeded. Consult the source code of `resample` for details.\n\nThe final example shows how to utilize `resample_poly` to speed up the\ndown-sampling: The input signal a non-zero value at :math:`t=0` and is downsampled\nfrom 19937 to 128 samples. Since 19937 is prime, the FFT is expected to be slow. To\nspeed matters up, `resample_poly` is used to downsample first by a factor of ``n0\n// n1 = 155`` and then pass the result to `resample`. Two parameterization of\n`resample_poly` are used: Passing ``padtype='wrap'`` treats the input as being\nperiodic whereas the default parametrization performs zero-padding. The upper\nsubplot shows the resulting signals over time whereas the lower subplot depicts the\nresulting one-sided magnitude spectra.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import matplotlib.pyplot as plt\nimport numpy as np\nfrom scipy.fft import rfftfreq, rfft\nfrom scipy.signal import resample, resample_poly\nn0 = 19937 # number of input samples - prime\nn1 = 128  # number of output samples - fast FFT length\nT0, T1 = 1/n0, 1/n1  # sampling intervals\nt0, t1 = np.arange(n0)*T0, np.arange(n1)*T1  # time stamps\nx0 = np.zeros(n0)  # Input has one non-zero sample\nx0[0] = n0\nx1r = resample(x0, n1)  # slow due to n0 being prime\nx1p = resample(resample_poly(x0, 1, n0 // n1, padtype='wrap'), n1)  # periodic\nx2p = resample(resample_poly(x0, 1, n0 // n1), n1)  # with zero-padding\nX0 = rfft(x0) / n0\nX1r, X1p, X2p = rfft(x1r) / n1, rfft(x1p) / n1, rfft(x2p) / n1\nf0, f1 = rfftfreq(n0, T0), rfftfreq(n1, T1)\nfig, (ax0, ax1) = plt.subplots(2, 1, layout='constrained', figsize=(5, 4))\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "ax0.set_title(f\"Dowsampled Impulse response (from {n0} to {n1} samples)\")\nax0.set(xlabel=\"Time $t$ in seconds\", ylabel=\"Amplitude $x(t)$\", xlim=(-T1, 1))\nfor x_ in (x1r, x1p, x2p):\n    ax0.plot(t1, x_, alpha=.5)\n",
        "execution_status": "failure",
        "out": "[<matplotlib.lines.Line2D object at 0x7f3f602bbe00>]\n[<matplotlib.lines.Line2D object at 0x7f3f60d40050>]\n[<matplotlib.lines.Line2D object at 0x7f3f60d401a0>]\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "ax0.grid()\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "ax1.set(xlabel=rf\"Frequency $f$ in hertz ($\\Delta f = {f1[1]}\\,$Hz)\",\n        ylabel=\"Magnitude $|X(f)|$\", xlim=(0, 0.55/T1))\nax1.axvspan(0.5/T1, f0[-1], color='gray', alpha=.2)\nax1.plot(f1, abs(X1r), 'C0.-', alpha=.5, label=\"resample\")\nax1.plot(f1, abs(X1p), 'C1.-', alpha=.5, label=\"resample_poly(padtype='wrap')\")\nax1.plot(f1, abs(X2p), 'C2x-', alpha=.5, label=\"resample_poly\")\n",
        "execution_status": "failure",
        "out": "[<matplotlib.lines.Line2D object at 0x7f3f60d40590>]\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "ax1.grid()\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "fig.legend(loc='outside lower center', ncols=2)\n",
        "execution_status": "failure",
        "out": "<matplotlib.legend.Legend object at 0x7f3f602bbb60>\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "plt.show()\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Figure",
        "__tag": 4024,
        "value": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "scipy",
          "version": "2.0.0.dev0+git20260907.41eeb59",
          "kind": "assets",
          "path": "fig-cca27473789c42e2.png"
        }
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe plots show that the results of the \"pure\" `resample` and the usage of the\ndefault parameters of `resample_poly` agree well.  The periodic padding of\n`resample_poly` (``padtype='wrap'``) on the other hand produces significant\ndeviations. This is caused by the disconiuity at the beginning of the signal, for\nwhich the default filter of `resample_poly` is not suited well. This example\nillustrates that for some use cases, adapting the `resample_poly` parameters may\nbe beneficial. `resample` has a big advantage in this regard: It uses the ideal\nantialiasing filter with the maximum bandwidth by default.\n\nNote that the doubled spectral magnitude at the Nyqist frequency of 64 Hz is due the\neven number of ``n1=128`` output samples, which requires a special treatment as\ndiscussed in the previous example."
      }
    ],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "decimate",
        "reference": {
          "__type": "LocalRef",
          "__tag": 4022,
          "kind": "module",
          "path": "scipy.signal._signaltools:decimate"
        },
        "kind": "module"
      },
      "descriptions": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Downsample a (periodic/non-periodic) signal after applying an FIR or IIR filter."
            }
          ]
        }
      ],
      "type": "func"
    },
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "resample_poly",
        "reference": {
          "__type": "LocalRef",
          "__tag": 4022,
          "kind": "module",
          "path": "scipy.signal._signaltools:resample_poly"
        },
        "kind": "module"
      },
      "descriptions": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Resample a (periodic/non-periodic) signal using polyphase filtering and an FIR filter."
            }
          ]
        }
      ],
      "type": "func"
    }
  ],
  "signature": {
    "__type": "SignatureNode",
    "__tag": 4029,
    "kind": "function",
    "parameters": [
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "x",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "num",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "t",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "None"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "axis",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "0"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "window",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "None"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "domain",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "time"
      }
    ],
    "return_annotation": {
      "__type": "Empty",
      "__tag": 4031
    },
    "target_name": "resample"
  },
  "references": null,
  "qa": "scipy.signal._signaltools:resample",
  "arbitrary": [],
  "local_refs": [
    "axis",
    "domain",
    "num",
    "t",
    "t_r",
    "window",
    "x",
    "x_r"
  ]
}