Librosa Constant Q Transform (CQT) contains defects at the beginning and ending of the spectrogram

Librosa Constant Q Transform (CQT) contains defects at the beginning and ending of the spectrogram

By : user3099746
Date : January 11 2021, 03:34 PM
I wish this help you I think you might want to try out pad_mode which is supported in cqt. If you checkout the np.pad documentation, you can see available options (or see the end of this post). With the wrap option, you get a result like this, though I suspect the phase is a mess, so you should make sure this meets your needs. If you are always generating your own signal, you could trying using the instead of one of the available options.
code :
import numpy as np
import matplotlib.pyplot as plt
from librosa import cqt

s = np.linspace(0,1,44100)
x = np.sin(2*np.pi*1000*s)

cq_lib = cqt(x,sr=44100, fmin=fmin, n_bins=40, pad_mode='wrap')

plt.imshow(abs(cq_lib),aspect='auto', origin='lower')
plt.xlabel('Time Steps')
plt.ylabel('Freq bins')

Share : facebook icon twitter icon
Using Librosa to plot a mel-spectrogram

Using Librosa to plot a mel-spectrogram

By : z6gcnm
Date : March 29 2020, 07:55 AM
To fix this issue oh, Your question is mainly about how to save it as jpg? If you just want to display pictures,You just need to add a line of code: plt.show()
if you want save a jpg, no axis, no white edge:
code :
import os
import matplotlib
matplotlib.use('Agg') # No pictures displayed 
import pylab
import librosa
import librosa.display
import numpy as np

sig, fs = librosa.load('path_to_my_wav_file')   
# make pictures name 
save_path = 'test.jpg'

pylab.axis('off') # no axis
pylab.axes([0., 0., 1., 1.], frameon=False, xticks=[], yticks=[]) # Remove the white edge
S = librosa.feature.melspectrogram(y=sig, sr=fs)
librosa.display.specshow(librosa.power_to_db(S, ref=np.max))
pylab.savefig(save_path, bbox_inches=None, pad_inches=0)
Why spectrogram from librosa library have twice the time duration of the actual audio track?

Why spectrogram from librosa library have twice the time duration of the actual audio track?

By : user1468855
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You need to pass the sampling rate to librosa.display.specshow (sr=self.SamplingFrequency). If not it defaults to 20050 and if self.SamplingFrequency is a different value, it will display the wrong length.
Difference between output of python librosa.core.stft() and matlab spectrogram(x)

Difference between output of python librosa.core.stft() and matlab spectrogram(x)

By : Stephen Ward
Date : March 29 2020, 07:55 AM
will be helpful for those in need After a long time, and an unsatisfied bounty, I have found the answer myself.
The MATLAB function spectrogram() outputs a vector of times which corresponds to the middle of each window while omitting the last window. For example, a 10 samples length signal with a 3 sample window and 1 sample overlap, will result in the following 4 windows:
convert spectrogram to audio using librosa functions

convert spectrogram to audio using librosa functions

By : user3197429
Date : March 29 2020, 07:55 AM
around this issue Your code works for me without error. I recommend reinstalling the latest version of librosa using a clean miniconda environment:
code :
conda install -c conda-forge librosa
How can I save a Librosa spectrogram plot as a specific sized image?

How can I save a Librosa spectrogram plot as a specific sized image?

By : Mike Erick
Date : January 02 2021, 06:48 AM
I wish this help you Plots are for humans to look at, and contains things like axis markers, labels etc that are not useful for machine learning. To feed a model with an 'image' of the spectrogram, one should output only the data. This data be stored in any format, but if you want to use a standard image format then should use PNG. Lossy compression such as JPEG introduces compression artifacts.
Here follows working example code to save spectrogram. Note that to get a fixed size image output, the code extracts a fixed-length window of the audio signal. Dividing an audio stream into such fixed-length analysis windows is standard practice.
code :
import librosa
import numpy
import skimage

def scale_minmax(X, min=0.0, max=1.0):
    X_std = (X - X.min()) / (X.max() - X.min())
    X_scaled = X_std * (max - min) + min
    return X_scaled

def spectrogram_image(y, sr, out, hop_length, n_mels):
    # use log-melspectrogram
    mels = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=n_mels,
                                            n_fft=hop_length*2, hop_length=hop_length)
    mels = numpy.log(mels + 1e-9) # add small number to avoid log(0)

    # min-max scale to fit inside 8-bit range
    img = scale_minmax(mels, 0, 255).astype(numpy.uint8)
    img = numpy.flip(img, axis=0) # put low frequencies at the bottom in image
    img = 255-img # invert. make black==more energy

    # save as PNG
    skimage.io.imsave(out, img)

if __name__ == '__main__':
    # settings
    hop_length = 512 # number of samples per time-step in spectrogram
    n_mels = 128 # number of bins in spectrogram. Height of image
    time_steps = 384 # number of time-steps. Width of image

    # load audio. Using example from librosa
    path = librosa.util.example_audio_file()
    y, sr = librosa.load(path, offset=1.0, duration=10.0, sr=22050)
    out = 'out.png'

    # extract a fixed length window
    start_sample = 0 # starting at beginning
    length_samples = time_steps*hop_length
    window = y[start_sample:start_sample+length_samples]

    # convert to PNG
    spectrogram_image(window, sr=sr, out=out, hop_length=hop_length, n_mels=n_mels)
    print('wrote file', out)
Related Posts Related Posts :
  • (Py)Tesseract failing to read text from simple image
  • Is there a reason why the following code does not execute (print the tweets) after taking input?
  • How can I use multiple bigquery projects together in python
  • Pandas merging rows with the same value and same index with multiple empty values
  • How to replace cell in pandas?
  • Can anybody explain me print statement in this code?
  • What is my problem to use of Conv2D for image?
  • Unable to import Pandas in AWS Lambda
  • Constant output of 2D Convolutional regression in Keras-tensorflow
  • Why can't replace placeholder with format function in pymysql?
  • Issue with div`s in Scraping with Python and Beautiful Soup
  • Can't change instance attribute of my class?
  • Add column to dataframe and merge
  • Indexing 3d numpy array with 2d array
  • Does using win32com library carry over privacy problems?
  • Something is adding upper folders in zip file
  • Is there a quick way to find the global quantile of a pandas dataframe?
  • Python 3 upgrade, a bytes-like object is required, not 'str'
  • How can I make a timer for a command without blocking the program
  • How to include first/last dates in matplotlib plot
  • What is the executable_path in Google Colaboratory for geckodriver?
  • Snakemake producing wildly incoherent error when dryrunning
  • How would I implement an ID to identify classes?
  • it shows"line 42, in <module> if input_ !='no': NameError: name 'input_' is not defined" when i giv no i
  • How get the text with BeautifulSoup in this html code: <span id="pass_0" class="text-success">
  • Trying to save instances in their class
  • Python Removing Words from list even if they match criteria
  • Text Classification with word2vec
  • How to implement rectangular pulses (discontinuities) on ODE right-hand side?
  • unpacking a 4-byte class gives a unpacking error
  • python3 take a callback that may take an argument and may not
  • How to make two iteration in for loop using for-in syntax
  • Finding Middle point of list in Python
  • using a for loop for web scraping - cannot "pass" certain data
  • Generate positive only distribution based on array
  • Why is numpy.random.choice modifying my data?
  • Pandas applymap loops twice, apply once?
  • Removing rows with specific text
  • Get the most repeated value from columns of list other than zero in pandas data frame
  • How to insert text in multiple files using python
  • Python merging excel files in directory
  • How to put the every start time as 0 in every day for specific column input data using panda python
  • Data Frame Error: UndefinedVariableError: name is not defined
  • Why won't a new line be created in this string? is it too long?
  • Python 3 - files imported as dictionary, but the values are lists - how to resolve?
  • Flask Tutorial: Could Not Import app in Visual Studio Code 1.37.1
  • 'TypeError: decoding str is not supported' when appending str in for loop within a for loop
  • How to scale a data using Python 3
  • How to create a matrix of characters with numpy broadcasting, meshgrid or other method
  • Is there any way of getting values from keys inside other keys?
  • Conditional Statements for dataframes
  • Python implementation of BFS to solve 8-puzzle takes too long to find a solution
  • Operand for matching any one of multiple cases
  • Is the rear item in a Queue the last item added or the item at the end of a Queue?
  • I am trying slicing but I have the following error message: slice indices must be integers or None or have an __index__
  • How to represent Binary tree into an array using python?
  • Vectorized implementation of field-aware factorization
  • 'Float' object has no attribute 'log'
  • pathlib mkdir creates a folder by filename
  • SyntaxError: invalid syntax for if statement
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org