René's URL Explorer Experiment


Title: [Bug]: Long audio files result in incomplete spectrogram visualizations · Issue #26368 · matplotlib/matplotlib · GitHub

Open Graph Title: [Bug]: Long audio files result in incomplete spectrogram visualizations · Issue #26368 · matplotlib/matplotlib

X Title: [Bug]: Long audio files result in incomplete spectrogram visualizations · Issue #26368 · matplotlib/matplotlib

Description: Bug summary When using matplotlib to generate spectrogram visualizations of audio files, if the audio file is too long, the spectrogram portion of the plot becomes blank towards the latter half, while the waveform continues to be display...

Open Graph Description: Bug summary When using matplotlib to generate spectrogram visualizations of audio files, if the audio file is too long, the spectrogram portion of the plot becomes blank towards the latter half, wh...

X Description: Bug summary When using matplotlib to generate spectrogram visualizations of audio files, if the audio file is too long, the spectrogram portion of the plot becomes blank towards the latter half, wh...

Opengraph URL: https://github.com/matplotlib/matplotlib/issues/26368

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: Long audio files result in incomplete spectrogram visualizations","articleBody":"### Bug summary\n\nWhen using matplotlib to generate spectrogram visualizations of audio files, if the audio file is too long, the spectrogram portion of the plot becomes blank towards the latter half, while the waveform continues to be displayed properly.\n\n### Code for reproduction\n\n```python\nimport torchaudio\r\nimport torchaudio.transforms as transforms\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nimport scipy.interpolate\r\nimport librosa.display\r\nimport matplotlib.gridspec as gridspec\r\n\r\nfrom tqdm import tqdm\r\nfrom concurrent.futures import ProcessPoolExecutor, as_completed\r\n\r\n\r\ndef compute_spectrogram_per_channel(channel_waveform, sample_rate):\r\n    # Create transformer to convert waveform to spectrogram\r\n    spectrogram_transform = transforms.Spectrogram(n_fft=2048, hop_length=256)\r\n\r\n    # Apply the transformer\r\n    spectrogram = spectrogram_transform(channel_waveform.unsqueeze(0))\r\n\r\n    amplitude_spectrogram = np.sqrt(spectrogram)\r\n    db_spectrogram = librosa.amplitude_to_db(amplitude_spectrogram[0].numpy(), ref=np.max)\r\n    db_spectrogram = np.clip(db_spectrogram, a_min=None, a_max=0)  # clip to 0dB\r\n\r\n    # Set new log scale\r\n    num_freqs, num_frames = db_spectrogram.shape\r\n    min_freq = 1  # human hearing range in Hz\r\n    max_freq = sample_rate / 2  # Nyquist frequency\r\n    frequencies = np.linspace(min_freq, max_freq, num=num_freqs)\r\n\r\n    # Create a new scale\r\n    log_scale = np.log10(frequencies)\r\n    linear_scale = np.linspace(np.log10(min_freq), np.log10(max_freq), num=num_freqs)\r\n    scale_ratio = 0.75  # adjust this parameter to control the ratio of log scale and linear scale\r\n    new_scale = scale_ratio * log_scale + (1 - scale_ratio) * linear_scale\r\n\r\n    new_db_spectrogram = np.empty_like(db_spectrogram)\r\n\r\n    # Apply interpolation for each frame\r\n    for frame in tqdm(range(num_frames)):\r\n        interpolator = scipy.interpolate.interp1d(log_scale, db_spectrogram[:, frame])\r\n        new_db_spectrogram[:, frame] = interpolator(new_scale)\r\n\r\n    return channel_waveform.t().numpy(), db_spectrogram, new_db_spectrogram\r\n\r\n\r\ndef plot_spectrogram(waveforms, new_db_spectrograms, audio_duration):\r\n    num_channels = len(waveforms)\r\n\r\n    # Create a plot, set the background to black and adjust the size based on audio duration\r\n    plt.figure(figsize=(max(audio_duration * 2, 10), 8), facecolor=\"black\")\r\n\r\n    # Dynamically create subplots based on the number of channels\r\n    gs = gridspec.GridSpec(2 * num_channels, 1, height_ratios=[1] * num_channels + [5] * num_channels)\r\n\r\n    # Loop through each channel to plot the waveform and spectrogram\r\n    for i in range(num_channels):\r\n        # Plot the waveform\r\n        ax_waveform = plt.subplot(gs[i])\r\n        ax_waveform.plot(waveforms[i], color=\"#4BF2A7\")\r\n        nonzero_indices = np.where(waveforms[i] != 0)[0]  # Find indices of non-zero values\r\n        ax_waveform.set_xlim(nonzero_indices[0], nonzero_indices[-1])  # Set x limit to range of non-zero values\r\n        ax_waveform.axis(\"off\")\r\n\r\n        # Plot the spectrogram\r\n        ax_spectrogram = plt.subplot(gs[i + num_channels])\r\n        ax_spectrogram.imshow(new_db_spectrograms[i], origin=\"lower\", aspect=\"auto\")\r\n        ax_spectrogram.axis(\"off\")\r\n\r\n    plt.subplots_adjust(left=0, right=1, top=1, bottom=0, wspace=0, hspace=0)  # Adjust to remove borders and gaps\r\n    plt.savefig(\"spectrogram.jpeg\", facecolor=\"black\", bbox_inches=\"tight\", pad_inches=0)  # Save the figure\r\n\r\n\r\ndef main(file):\r\n    # Load the audio file\r\n    waveform, sample_rate = torchaudio.load(file)\r\n\r\n    num_channels = waveform.shape[0]\r\n    audio_duration = waveform.shape[1] / sample_rate  # Calculate audio duration\r\n\r\n    with ProcessPoolExecutor() as executor:\r\n        futures = {\r\n            executor.submit(compute_spectrogram_per_channel, waveform[ch], sample_rate): ch\r\n            for ch in range(num_channels)\r\n        }\r\n        waveforms = [None] * num_channels\r\n        db_spectrograms = [None] * num_channels\r\n        new_db_spectrograms = [None] * num_channels\r\n        for future in as_completed(futures):\r\n            ch = futures[future]\r\n            waveforms[ch], db_spectrograms[ch], new_db_spectrograms[ch] = future.result()\r\n\r\n    # Adjust figure width based on audio duration\r\n    plot_spectrogram(waveforms, new_db_spectrograms, audio_duration)\r\n\r\n\r\n# Run the main function\r\nif __name__ == \"__main__\":\r\n    file = \"long.wav\"\r\n    main(file)\n```\n\n\n### Actual outcome\n\nWhen visualizing an audio file of significant length, the spectrogram does not render correctly for the entire duration of the audio. The latter part of the spectrogram is blank and contains no information, which doesn't match with the waveform visualization, which continues to display normally.\r\n\r\nlong wave\r\n![spectrogram](https://github.com/matplotlib/matplotlib/assets/59153990/1abda313-8ca3-4f8d-b398-3bb1b5290b99)\r\n\r\nshort wave\r\n![spectrogram](https://github.com/matplotlib/matplotlib/assets/59153990/23635ce8-6156-4d5e-8cd1-8f7dbc0fedcc)\r\n\n\n### Expected outcome\n\nThe spectrogram should be consistently rendered for the entire duration of the audio file, matching the waveform visualization. Regardless of the length of the audio file, the spectrogram should be complete and not become blank at any point.\n\n### Additional information\n\n - Other libraries involved: torchaudio, librosa, numpy, scipy\r\n\r\nThis issue occurs regardless of the audio file format or the specifics of the audio content. It seems directly related to the length of the audio file.\n\n### Operating system\n\nWindows \u0026 Ubuntu\n\n### Matplotlib Version\n\n3.7.2\n\n### Matplotlib Backend\n\nTkAgg\n\n### Python version\n\nPython 3.10.10\n\n### Jupyter version\n\n_No response_\n\n### Installation\n\npip","author":{"url":"https://github.com/djkcyl","@type":"Person","name":"djkcyl"},"datePublished":"2023-07-21T04:06:14.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":13},"url":"https://github.com/26368/matplotlib/issues/26368"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:4d55bc21-0a7a-d596-73b0-3a103d4b9ed7
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9D46:1BB425:2D6909:3CEF36:6A52EB6A
html-safe-nonceb215c0c54bd6789500a8d7c90b4900ab26b97820638129729502c34a0f84fe70
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RDQ2OjFCQjQyNToyRDY5MDk6M0NFRjM2OjZBNTJFQjZBIiwidmlzaXRvcl9pZCI6IjQ3ODYwNTM3MzM4NTQ0MDc1MzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac106dbecd9dcff2a3f2218576c8b84315dad45a6189e51503b6b8ded7dd2457aa
hovercard-subject-tagissue:1815119190
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/26368/issue_layout
twitter:imagehttps://opengraph.githubassets.com/4e0e0bd3571347c61f72ea30805b1b408fcd8188f6c2bbd079998c3a098192e5/matplotlib/matplotlib/issues/26368
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/4e0e0bd3571347c61f72ea30805b1b408fcd8188f6c2bbd079998c3a098192e5/matplotlib/matplotlib/issues/26368
og:image:altBug summary When using matplotlib to generate spectrogram visualizations of audio files, if the audio file is too long, the spectrogram portion of the plot becomes blank towards the latter half, wh...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamedjkcyl
hostnamegithub.com
expected-hostnamegithub.com
Noneb9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb
turbo-cache-controlno-preview
go-importgithub.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git
octolytics-dimension-user_id215947
octolytics-dimension-user_loginmatplotlib
octolytics-dimension-repository_id1385122
octolytics-dimension-repository_nwomatplotlib/matplotlib
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id1385122
octolytics-dimension-repository_network_root_nwomatplotlib/matplotlib
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release07a982c1d40157c619b364352b704c3ce66bb332
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/matplotlib/matplotlib/issues/26368#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmatplotlib%2Fissues%2F26368
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmatplotlib%2Fissues%2F26368
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=matplotlib%2Fmatplotlib
Reloadhttps://github.com/matplotlib/matplotlib/issues/26368
Reloadhttps://github.com/matplotlib/matplotlib/issues/26368
Reloadhttps://github.com/matplotlib/matplotlib/issues/26368
Please reload this pagehttps://github.com/matplotlib/matplotlib/issues/26368
matplotlib https://github.com/matplotlib
matplotlibhttps://github.com/matplotlib/matplotlib
Please reload this pagehttps://github.com/matplotlib/matplotlib/issues/26368
Notifications https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Fork 8.4k https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Star 23k https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Code https://github.com/matplotlib/matplotlib
Issues 1.1k https://github.com/matplotlib/matplotlib/issues
Pull requests 408 https://github.com/matplotlib/matplotlib/pulls
Actions https://github.com/matplotlib/matplotlib/actions
Projects https://github.com/matplotlib/matplotlib/projects
Wiki https://github.com/matplotlib/matplotlib/wiki
Security and quality 0 https://github.com/matplotlib/matplotlib/security
Insights https://github.com/matplotlib/matplotlib/pulse
Code https://github.com/matplotlib/matplotlib
Issues https://github.com/matplotlib/matplotlib/issues
Pull requests https://github.com/matplotlib/matplotlib/pulls
Actions https://github.com/matplotlib/matplotlib/actions
Projects https://github.com/matplotlib/matplotlib/projects
Wiki https://github.com/matplotlib/matplotlib/wiki
Security and quality https://github.com/matplotlib/matplotlib/security
Insights https://github.com/matplotlib/matplotlib/pulse
#28904https://github.com/matplotlib/matplotlib/pull/28904
[Bug]: Long audio files result in incomplete spectrogram visualizationshttps://github.com/matplotlib/matplotlib/issues/26368#top
#28904https://github.com/matplotlib/matplotlib/pull/28904
Performancehttps://github.com/matplotlib/matplotlib/issues?q=state%3Aopen%20label%3A%22Performance%22
v3.10.0https://github.com/matplotlib/matplotlib/milestone/84
https://github.com/djkcyl
djkcylhttps://github.com/djkcyl
on Jul 21, 2023https://github.com/matplotlib/matplotlib/issues/26368#issue-1815119190
https://private-user-images.githubusercontent.com/59153990/255069363-1abda313-8ca3-4f8d-b398-3bb1b5290b99.jpeg?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3ODM4MTk0MTUsIm5iZiI6MTc4MzgxOTExNSwicGF0aCI6Ii81OTE1Mzk5MC8yNTUwNjkzNjMtMWFiZGEzMTMtOGNhMy00ZjhkLWIzOTgtM2JiMWI1MjkwYjk5LmpwZWc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwNzEyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDcxMlQwMTE4MzVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1kMzdiNjdlMWI1ZWE3YmExNzY3YzRkNWNhZTA3MTAyZTk0MTYzMTZkNjNmYzM4MzQ1ZDU2NjQ2YWM0MTU1ZGE4JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZyZXNwb25zZS1jb250ZW50LXR5cGU9aW1hZ2UlMkZqcGVnIn0.dmkrKECJ-fPfbVSHZiu9w3bTTnHWoZDqUT3tAI9pKUo
https://private-user-images.githubusercontent.com/59153990/255069626-23635ce8-6156-4d5e-8cd1-8f7dbc0fedcc.jpeg?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3ODM4MTk0MTUsIm5iZiI6MTc4MzgxOTExNSwicGF0aCI6Ii81OTE1Mzk5MC8yNTUwNjk2MjYtMjM2MzVjZTgtNjE1Ni00ZDVlLThjZDEtOGY3ZGJjMGZlZGNjLmpwZWc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwNzEyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDcxMlQwMTE4MzVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0yNzgyY2FhZjhjMmQ2MTZkMDI5MGJlMjJiZjIxNzNhNDkzNzE4Mzk1YTE5NzI4OWZiMmQ4NTNmMDA5ZTNmMGNhJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZyZXNwb25zZS1jb250ZW50LXR5cGU9aW1hZ2UlMkZqcGVnIn0.R49D0xSAukul8y034-mkWCQImoO8095TCJox3oaJy1Y
Performancehttps://github.com/matplotlib/matplotlib/issues?q=state%3Aopen%20label%3A%22Performance%22
v3.10.0https://github.com/matplotlib/matplotlib/milestone/84
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.