{
  "id": 269871,
  "title": "pytorch bandpass filter",
  "url": "/competitions/g2net-gravitational-wave-detection/discussion/269871",
  "author_name": "",
  "post_date": "2021-09-02T12:53:06.242185100Z",
  "votes": 9,
  "comment_count": 13,
  "views": 0,
  "content": "<p><a href=\"https://www.kaggle.com/c/g2net-gravitational-wave-detection/discussion/265367#1476566\" target=\"_blank\">This post</a> was made a while ago, but Torch audio's bandpass_biquad output nothing like scipy.signal's filtfilt+butter. The high and low frequency values do not seem to be respected at all. Does anyone have a working Butterworth BP filter in pytorch similar to scipy's?</p>\n<p><img src=\"https://i.imgur.com/8KDF7lL.png\" alt=\"\"></p>",
  "messages": [
    {
      "id": "1500516",
      "postDate": "09/02/2021 12:53:06",
      "content": "<p><a href=\"https://www.kaggle.com/c/g2net-gravitational-wave-detection/discussion/265367#1476566\" target=\"_blank\">This post</a> was made a while ago, but Torch audio's bandpass_biquad output nothing like scipy.signal's filtfilt+butter. The high and low frequency values do not seem to be respected at all. Does anyone have a working Butterworth BP filter in pytorch similar to scipy's?</p>\n<p><img src=\"https://i.imgur.com/8KDF7lL.png\" alt=\"\"></p>",
      "rawMarkdown": "[This post](https://www.kaggle.com/c/g2net-gravitational-wave-detection/discussion/265367#1476566) was made a while ago, but Torch audio's bandpass_biquad output nothing like scipy.signal's filtfilt+butter. The high and low frequency values do not seem to be respected at all. Does anyone have a working Butterworth BP filter in pytorch similar to scipy's?\n\n![](https://i.imgur.com/8KDF7lL.png)",
      "votes": null
    },
    {
      "id": "1504678",
      "postDate": "09/06/2021 15:12:12",
      "content": "<p>How similar do the plots look, if you use the same y axis scale? (The top plot is zoomed is so it will show more wiggles whereas the bottom plot is zoomed out)</p>",
      "rawMarkdown": "How similar do the plots look, if you use the same y axis scale? (The top plot is zoomed is so it will show more wiggles whereas the bottom plot is zoomed out)",
      "votes": null
    },
    {
      "id": "1504703",
      "postDate": "09/06/2021 15:30:47",
      "content": "<p>Hi Chris, always a pleasure to see you.</p>\n<p><img src=\"https://i.imgur.com/SfgVtas.png\" alt=\"\"></p>\n<p>I truncated the first 5 values on each signal:</p>\n<pre><code>from scipy import signal\nfrom torchaudio.functional import bandpass_biquad\nimport torch\n\ndef butter_bandpass_filter(data, lowcut, highcut, fs):\n    return bandpass_biquad(data, fs, (highcut + lowcut) / 2, (highcut - lowcut) / (highcut + lowcut))\ndef apply_bandpass(x, lf=25, hf=500, order=4, sr=2048):\n    sos = signal.butter(order, [lf, hf], btype=\"bandpass\", output=\"sos\", fs=sr)\n    normalization = np.sqrt((hf - lf) / (sr / 2))\n    return signal.sosfiltfilt(sos, x) / normalization\n\nv1 = butter_bandpass_filter(torch.from_numpy(arr), 20, 500, 2048) \nv2 = apply_bandpass(arr, 20,500)\n</code></pre>",
      "rawMarkdown": "Hi Chris, always a pleasure to see you.\n\n![](https://i.imgur.com/SfgVtas.png)\n\nI truncated the first 5 values on each signal:\n\n```\nfrom scipy import signal\nfrom torchaudio.functional import bandpass_biquad\nimport torch\n\ndef butter_bandpass_filter(data, lowcut, highcut, fs):\n    return bandpass_biquad(data, fs, (highcut + lowcut) / 2, (highcut - lowcut) / (highcut + lowcut))\ndef apply_bandpass(x, lf=25, hf=500, order=4, sr=2048):\n    sos = signal.butter(order, [lf, hf], btype=\"bandpass\", output=\"sos\", fs=sr)\n    normalization = np.sqrt((hf - lf) / (sr / 2))\n    return signal.sosfiltfilt(sos, x) / normalization\n\nv1 = butter_bandpass_filter(torch.from_numpy(arr), 20, 500, 2048) \nv2 = apply_bandpass(arr, 20,500)\n```",
      "votes": null
    },
    {
      "id": "1505993",
      "postDate": "09/07/2021 18:04:30",
      "content": "<p>I am not sure if this is useful <a href=\"https://github.com/adefossez/julius/blob/main/julius/filters.py\" target=\"_blank\">https://github.com/adefossez/julius/blob/main/julius/filters.py</a></p>",
      "rawMarkdown": "I am not sure if this is useful https://github.com/adefossez/julius/blob/main/julius/filters.py",
      "votes": null
    },
    {
      "id": "1506125",
      "postDate": "09/07/2021 21:56:02",
      "content": "<p>I'll add it to the ever growing experiments list. My goal would be have the network learn what the best cutoff frequencies for the dataset are rather than us managing that by hand. In theory, once we have those values, we don't need to make those params trainable anymore.</p>",
      "rawMarkdown": "I'll add it to the ever growing experiments list. My goal would be have the network learn what the best cutoff frequencies for the dataset are rather than us managing that by hand. In theory, once we have those values, we don't need to make those params trainable anymore.",
      "votes": null
    },
    {
      "id": "1507716",
      "postDate": "09/09/2021 13:23:06",
      "content": "<p>It's just different filters with different application methods. You set butter order to 4, but as far as I see biquad have order of 2, also filtfilt applies filter twice - forward and backwards, so effective order of butter is 8. Maybe try to apply biquad few times or try to adjust it's order?</p>",
      "rawMarkdown": "It's just different filters with different application methods. You set butter order to 4, but as far as I see biquad have order of 2, also filtfilt applies filter twice - forward and backwards, so effective order of butter is 8. Maybe try to apply biquad few times or try to adjust it's order?",
      "votes": null
    },
    {
      "id": "1507720",
      "postDate": "09/09/2021 13:30:27",
      "content": "<p>Thank you for this hint. My DSP knowledge is lacking, it seems its time to invest a little into it. Also, welcome to the discussion.. =)</p>",
      "rawMarkdown": "Thank you for this hint. My DSP knowledge is lacking, it seems its time to invest a little into it. Also, welcome to the discussion.. =)",
      "votes": null
    },
    {
      "id": "1516812",
      "postDate": "09/18/2021 18:05:26",
      "content": "<p><a href=\"https://www.kaggle.com/authman\" target=\"_blank\">@authman</a> are you saying pytorch biquad not able to give same output as scipy butter filter ?</p>",
      "rawMarkdown": "authman are you saying pytorch biquad not able to give same output as scipy butter filter ?",
      "votes": null
    },
    {
      "id": "1516816",
      "postDate": "09/18/2021 18:11:38",
      "content": "<p><a href=\"https://www.kaggle.com/denisbsu\" target=\"_blank\">@denisbsu</a> does order matters in scipy butter worth filter ?</p>",
      "rawMarkdown": "denisbsu does order matters in scipy butter worth filter ?",
      "votes": null
    },
    {
      "id": "1519338",
      "postDate": "09/21/2021 14:37:42",
      "content": "<p>Yes - it's basically how strong your filter is (and how bad would border effects be).</p>",
      "rawMarkdown": "Yes - it's basically how strong your filter is (and how bad would border effects be).",
      "votes": null
    },
    {
      "id": "1519429",
      "postDate": "09/21/2021 15:48:55",
      "content": "<p>\" how bad would border effects\"</p>\n<p>i am surprised that conv cnn (which can understand border via padding) and transformer (with position encoding) cannot reduce the border noise</p>",
      "rawMarkdown": "\" how bad would border effects\"\n\ni am surprised that conv cnn (which can understand border via padding) and transformer (with position encoding) cannot reduce the border noise",
      "votes": null
    },
    {
      "id": "1519484",
      "postDate": "09/21/2021 16:46:53",
      "content": "<p>i see its 4 and 8 that are used in kernels . Is there a trade off when we go higher ?</p>",
      "rawMarkdown": "i see its 4 and 8 that are used in kernels . Is there a trade off when we go higher ?",
      "votes": null
    },
    {
      "id": "1520472",
      "postDate": "09/22/2021 10:45:01",
      "content": "<p>4 with filtfilt or 8 plain would give you 8 * 6 = 48 db low frequency noise attenuation (about 200 times amplitude reduction). Looks like good enough. Drawbacks of going higher are more computation time and potentially more distortion in first and last few milliseconds.</p>",
      "rawMarkdown": "4 with filtfilt or 8 plain would give you 8 * 6 = 48 db low frequency noise attenuation (about 200 times amplitude reduction). Looks like good enough. Drawbacks of going higher are more computation time and potentially more distortion in first and last few milliseconds.",
      "votes": null
    },
    {
      "id": "1559730",
      "postDate": "10/27/2021 07:09:56",
      "content": "<p>Hey All,</p>\n<p>Thank you all for taking part in our competition. The participation has been overwhelmingly positive. We are currently conducting a survey to gauge the demographic and outreach achieved. Kindly spare 2min and fill in this survey <a href=\"https://forms.gle/QP9L16niPexozyhu5\" target=\"_blank\">https://forms.gle/QP9L16niPexozyhu5</a>.</p>\n<p>Thank you all,</p>\n<p>Regards,<br>\nChris</p>",
      "rawMarkdown": "Hey All,\n\nThank you all for taking part in our competition. The participation has been overwhelmingly positive. We are currently conducting a survey to gauge the demographic and outreach achieved. Kindly spare 2min and fill in this survey https://forms.gle/QP9L16niPexozyhu5.\n\nThank you all,\n\nRegards,\nChris",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1504678,
      "author_name": "cdeotte",
      "author_url": "",
      "post_date": "09/06/2021 15:12:12",
      "content": "<p>How similar do the plots look, if you use the same y axis scale? (The top plot is zoomed is so it will show more wiggles whereas the bottom plot is zoomed out)</p>",
      "votes": null,
      "replies": [
        {
          "id": 1504703,
          "author_name": "authman",
          "author_url": "",
          "post_date": "09/06/2021 15:30:47",
          "content": "<p>Hi Chris, always a pleasure to see you.</p>\n<p><img src=\"https://i.imgur.com/SfgVtas.png\" alt=\"\"></p>\n<p>I truncated the first 5 values on each signal:</p>\n<pre><code>from scipy import signal\nfrom torchaudio.functional import bandpass_biquad\nimport torch\n\ndef butter_bandpass_filter(data, lowcut, highcut, fs):\n    return bandpass_biquad(data, fs, (highcut + lowcut) / 2, (highcut - lowcut) / (highcut + lowcut))\ndef apply_bandpass(x, lf=25, hf=500, order=4, sr=2048):\n    sos = signal.butter(order, [lf, hf], btype=\"bandpass\", output=\"sos\", fs=sr)\n    normalization = np.sqrt((hf - lf) / (sr / 2))\n    return signal.sosfiltfilt(sos, x) / normalization\n\nv1 = butter_bandpass_filter(torch.from_numpy(arr), 20, 500, 2048) \nv2 = apply_bandpass(arr, 20,500)\n</code></pre>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1505993,
      "author_name": "ggrizzly",
      "author_url": "",
      "post_date": "09/07/2021 18:04:30",
      "content": "<p>I am not sure if this is useful <a href=\"https://github.com/adefossez/julius/blob/main/julius/filters.py\" target=\"_blank\">https://github.com/adefossez/julius/blob/main/julius/filters.py</a></p>",
      "votes": null,
      "replies": [
        {
          "id": 1506125,
          "author_name": "authman",
          "author_url": "",
          "post_date": "09/07/2021 21:56:02",
          "content": "<p>I'll add it to the ever growing experiments list. My goal would be have the network learn what the best cutoff frequencies for the dataset are rather than us managing that by hand. In theory, once we have those values, we don't need to make those params trainable anymore.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1507716,
      "author_name": "denisbsu",
      "author_url": "",
      "post_date": "09/09/2021 13:23:06",
      "content": "<p>It's just different filters with different application methods. You set butter order to 4, but as far as I see biquad have order of 2, also filtfilt applies filter twice - forward and backwards, so effective order of butter is 8. Maybe try to apply biquad few times or try to adjust it's order?</p>",
      "votes": null,
      "replies": [
        {
          "id": 1507720,
          "author_name": "authman",
          "author_url": "",
          "post_date": "09/09/2021 13:30:27",
          "content": "<p>Thank you for this hint. My DSP knowledge is lacking, it seems its time to invest a little into it. Also, welcome to the discussion.. =)</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 1516816,
          "author_name": "jaideepvalani",
          "author_url": "",
          "post_date": "09/18/2021 18:11:38",
          "content": "<p><a href=\"https://www.kaggle.com/denisbsu\" target=\"_blank\">@denisbsu</a> does order matters in scipy butter worth filter ?</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 1519338,
          "author_name": "denisbsu",
          "author_url": "",
          "post_date": "09/21/2021 14:37:42",
          "content": "<p>Yes - it's basically how strong your filter is (and how bad would border effects be).</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 1519429,
          "author_name": "hengck23",
          "author_url": "",
          "post_date": "09/21/2021 15:48:55",
          "content": "<p>\" how bad would border effects\"</p>\n<p>i am surprised that conv cnn (which can understand border via padding) and transformer (with position encoding) cannot reduce the border noise</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 1519484,
          "author_name": "jaideepvalani",
          "author_url": "",
          "post_date": "09/21/2021 16:46:53",
          "content": "<p>i see its 4 and 8 that are used in kernels . Is there a trade off when we go higher ?</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 1520472,
          "author_name": "denisbsu",
          "author_url": "",
          "post_date": "09/22/2021 10:45:01",
          "content": "<p>4 with filtfilt or 8 plain would give you 8 * 6 = 48 db low frequency noise attenuation (about 200 times amplitude reduction). Looks like good enough. Drawbacks of going higher are more computation time and potentially more distortion in first and last few milliseconds.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1516812,
      "author_name": "jaideepvalani",
      "author_url": "",
      "post_date": "09/18/2021 18:05:26",
      "content": "<p><a href=\"https://www.kaggle.com/authman\" target=\"_blank\">@authman</a> are you saying pytorch biquad not able to give same output as scipy butter filter ?</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1559730,
      "author_name": "zerafachris",
      "author_url": "",
      "post_date": "10/27/2021 07:09:56",
      "content": "<p>Hey All,</p>\n<p>Thank you all for taking part in our competition. The participation has been overwhelmingly positive. We are currently conducting a survey to gauge the demographic and outreach achieved. Kindly spare 2min and fill in this survey <a href=\"https://forms.gle/QP9L16niPexozyhu5\" target=\"_blank\">https://forms.gle/QP9L16niPexozyhu5</a>.</p>\n<p>Thank you all,</p>\n<p>Regards,<br>\nChris</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1500516": "[This post](https://www.kaggle.com/c/g2net-gravitational-wave-detection/discussion/265367#1476566) was made a while ago, but Torch audio's bandpass_biquad output nothing like scipy.signal's filtfilt+butter. The high and low frequency values do not seem to be respected at all. Does anyone have a working Butterworth BP filter in pytorch similar to scipy's?\n\n![](https://i.imgur.com/8KDF7lL.png)",
    "1504678": "How similar do the plots look, if you use the same y axis scale? (The top plot is zoomed is so it will show more wiggles whereas the bottom plot is zoomed out)",
    "1504703": "Hi Chris, always a pleasure to see you.\n\n![](https://i.imgur.com/SfgVtas.png)\n\nI truncated the first 5 values on each signal:\n\n```\nfrom scipy import signal\nfrom torchaudio.functional import bandpass_biquad\nimport torch\n\ndef butter_bandpass_filter(data, lowcut, highcut, fs):\n    return bandpass_biquad(data, fs, (highcut + lowcut) / 2, (highcut - lowcut) / (highcut + lowcut))\ndef apply_bandpass(x, lf=25, hf=500, order=4, sr=2048):\n    sos = signal.butter(order, [lf, hf], btype=\"bandpass\", output=\"sos\", fs=sr)\n    normalization = np.sqrt((hf - lf) / (sr / 2))\n    return signal.sosfiltfilt(sos, x) / normalization\n\nv1 = butter_bandpass_filter(torch.from_numpy(arr), 20, 500, 2048) \nv2 = apply_bandpass(arr, 20,500)\n```",
    "1505993": "I am not sure if this is useful https://github.com/adefossez/julius/blob/main/julius/filters.py",
    "1506125": "I'll add it to the ever growing experiments list. My goal would be have the network learn what the best cutoff frequencies for the dataset are rather than us managing that by hand. In theory, once we have those values, we don't need to make those params trainable anymore.",
    "1507716": "It's just different filters with different application methods. You set butter order to 4, but as far as I see biquad have order of 2, also filtfilt applies filter twice - forward and backwards, so effective order of butter is 8. Maybe try to apply biquad few times or try to adjust it's order?",
    "1507720": "Thank you for this hint. My DSP knowledge is lacking, it seems its time to invest a little into it. Also, welcome to the discussion.. =)",
    "1516812": "authman are you saying pytorch biquad not able to give same output as scipy butter filter ?",
    "1516816": "denisbsu does order matters in scipy butter worth filter ?",
    "1519338": "Yes - it's basically how strong your filter is (and how bad would border effects be).",
    "1519429": "\" how bad would border effects\"\n\ni am surprised that conv cnn (which can understand border via padding) and transformer (with position encoding) cannot reduce the border noise",
    "1519484": "i see its 4 and 8 that are used in kernels . Is there a trade off when we go higher ?",
    "1520472": "4 with filtfilt or 8 plain would give you 8 * 6 = 48 db low frequency noise attenuation (about 200 times amplitude reduction). Looks like good enough. Drawbacks of going higher are more computation time and potentially more distortion in first and last few milliseconds.",
    "1559730": "Hey All,\n\nThank you all for taking part in our competition. The participation has been overwhelmingly positive. We are currently conducting a survey to gauge the demographic and outreach achieved. Kindly spare 2min and fill in this survey https://forms.gle/QP9L16niPexozyhu5.\n\nThank you all,\n\nRegards,\nChris"
  },
  "source": "meta"
}