Python matplotlib.pyplot.specgram() Examples

The following are code examples for showing how to use matplotlib.pyplot.specgram(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: SiRe   Author: RasmusD   File: visualisation_tools.py    Apache License 2.0 6 votes vote down vote up
def plot_spectogram(wav_path, f0=None):
  wav =get_wav(wav_path)
  fs = wav.getframerate()
  nf = wav.getnframes()
  ns = nf/float(fs)
  wav = fromstring(wav.readframes(-1), 'Int16')
  pyplot.figure(1)
  pyplot.title(wav_path)
  pyplot.specgram(wav, Fs=fs)
  pyplot.xlim(right=ns)
  pyplot.ylim(top=8000)
  pyplot.xlabel("Seconds")
  if f0:
    x_points = [(ns/len(f0))*x for x in range(1, len(f0)+1)]
    y_points = [x for x in f0]
    pyplot.plot(x_points, y_points)
  pyplot.show() 
Example 2
Project: SiRe   Author: RasmusD   File: visualisation_tools.py    Apache License 2.0 6 votes vote down vote up
def plot_wav_and_spec(wav_path, f0):
  wav = get_wav(wav_path)
  fs = wav.getframerate()
  nf = wav.getnframes()
  ns = nf/float(fs)
  wav = fromstring(wav.readframes(-1), 'Int16')
  
  fig = pyplot.figure()
  pyplot.title(wav_path)
  w = pyplot.subplot(311)
  w.set_xlim(right=nf)
  w.plot(wav)
  pyplot.xlabel("Frames")
  s = pyplot.subplot(312)
  pyplot.specgram(wav, Fs=fs)
  s.set_xlim(right=ns)
  s.set_ylim(top=8000)
  if f0:
    f = pyplot.subplot(313)
    x_points = [(ns/len(f0))*x for x in range(1, len(f0)+1)]
    y_points = [x for x in f0]
    pyplot.plot(x_points, y_points)
    f.set_xlim(right=ns)
  pyplot.xlabel("Seconds")
  pyplot.show() 
Example 3
Project: vibe   Author: 3ll3d00d   File: graphs.py    MIT License 6 votes vote down vote up
def showSpectro(self):
        # measurementPath = 'C:\\Users\\\Matt\\OneDrive\\Documents\\eot\\Edge of Tomorrow - Opening.wav'
        measurementPath = os.path.join(os.path.dirname(__file__), 'data', 'eot.wav')
        measurement = ms.loadSignalFromWav(measurementPath)

        # t, f, Sxx_spec = measurement.spectrogram()
        # plt.pcolormesh(f, t, Sxx_spec)
        # plt.ylim(0, 160)
        cmap = plt.get_cmap('viridis')
        cmap.set_under(color='k', alpha=None)
        plt.specgram(measurement.samples,
                     NFFT=measurement.getSegmentLength(),
                     Fs=measurement.fs,
                     detrend=mlab.detrend_none,
                     mode='magnitude',
                     noverlap=measurement.getSegmentLength() / 2,
                     window=mlab.window_hanning,
                     vmin=-60,
                     cmap=plt.cm.gist_heat)
        plt.ylim(0, 100)
        plt.show() 
Example 4
Project: tools   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run_phase_reconstruction_example():
    fs, d = fetch_sample_speech_tapestry()
    # actually gives however many components you say! So double what .m file
    # says
    fftsize = 512
    step = 64
    X_s = np.abs(stft(d, fftsize=fftsize, step=step, real=False,
                      compute_onesided=False))
    X_t = iterate_invert_spectrogram(X_s, fftsize, step, verbose=True)

    """
    import matplotlib.pyplot as plt
    plt.specgram(d, cmap="gray")
    plt.savefig("1.png")
    plt.close()
    plt.imshow(X_s, cmap="gray")
    plt.savefig("2.png")
    plt.close()
    """

    wavfile.write("phase_original.wav", fs, soundsc(d))
    wavfile.write("phase_reconstruction.wav", fs, soundsc(X_t)) 
Example 5
Project: irasl2018   Author: jfsantos   File: audio_tools.py    MIT License 6 votes vote down vote up
def run_phase_reconstruction_example():
    fs, d = fetch_sample_speech_tapestry()
    # actually gives however many components you say! So double what .m file
    # says
    fftsize = 512
    step = 64
    X_s = np.abs(stft(d, fftsize=fftsize, step=step, real=False,
                      compute_onesided=False))
    X_t = iterate_invert_spectrogram(X_s, fftsize, step, verbose=True)

    """
    import matplotlib.pyplot as plt
    plt.specgram(d, cmap="gray")
    plt.savefig("1.png")
    plt.close()
    plt.imshow(X_s, cmap="gray")
    plt.savefig("2.png")
    plt.close()
    """

    wavfile.write("phase_original.wav", fs, soundsc(d))
    wavfile.write("phase_reconstruction.wav", fs, soundsc(X_t)) 
Example 6
Project: Audio2Spectrogram   Author: vishnu-ks   File: Audio2Spectrogram.py    MIT License 6 votes vote down vote up
def graph_spectrogram(wav_file):
    rate, data = get_wav_info(wav_file)
    print type(data),len(data)
    nfft = 256  # Length of the windowing segments
    fs = 256    # Sampling frequency
    pxx, freqs, bins, im = plt.specgram(data, nfft,fs)
    print "pxx : ",len(pxx)
    print "freqs : ",len(freqs)
    print "bins : ",len(bins)
    #plt.axis('on')
    #plt.show()
    plt.axis('off')
    plt.savefig(wav_file.split('.')[0]+'.png',
                dpi=100, # Dots per inch
                frameon='false',
                aspect='normal',
                bbox_inches='tight',
                pad_inches=0) # Spectrogram saved as a .png
    im=Image.open(wav_file.split('.')[0]+'.png')
    rgb_im=im.convert('RGB')
    rgb_im.save(wav_file.split('.')[0]+'.jpg')
    if os.path.exists(wav_file.split('.')[0]+'.png'):
      os.remove(wav_file.split('.')[0]+'.png') 
Example 7
Project: Audio2Spectrogram   Author: vishnu-ks   File: Audio2Spectrogram.py    MIT License 6 votes vote down vote up
def graph_spectrogram(wav_file):
    rate, data = get_wav_info(wav_file)
    print type(data),len(data)
    nfft = 256  # Length of the windowing segments
    fs = 256    # Sampling frequency
    pxx, freqs, bins, im = plt.specgram(data, nfft,fs)
    print "pxx : ",len(pxx)
    print "freqs : ",len(freqs)
    print "bins : ",len(bins)
    #plt.axis('on')
    #plt.show()
    plt.axis('off')
    plt.savefig(wav_file.split('.')[0]+'.png',
                dpi=100, # Dots per inch
                frameon='false',
                aspect='normal',
                bbox_inches='tight',
                pad_inches=0) # Spectrogram saved as a .png
    im=Image.open(wav_file.split('.')[0]+'.png')
    rgb_im=im.convert('RGB')
    rgb_im.save(wav_file.split('.')[0]+'.jpg')
    if os.path.exists(wav_file.split('.')[0]+'.png'):
      os.remove(wav_file.split('.')[0]+'.png') 
Example 8
Project: synthesizer   Author: irmen   File: synth_demo.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def harmonics():
    synth = WaveSynth()
    freq = 1500
    num_harmonics = 6
    h_all = synth.harmonics(freq, 1, [(n, 1/n) for n in range(1, num_harmonics+1)])
    even_harmonics = [(1, 1)]  # always include fundamental tone harmonic
    even_harmonics.extend([(n, 1/n) for n in range(2, num_harmonics*2, 2)])
    h_even = synth.harmonics(freq, 1, even_harmonics)
    h_odd = synth.harmonics(freq, 1, [(n, 1/n) for n in range(1, num_harmonics*2, 2)])
    h_all.join(h_even).join(h_odd)
    import matplotlib.pyplot as plot
    plot.title("Spectrogram")
    plot.ylabel("Freq")
    plot.xlabel("Time")
    plot.specgram(h_all.get_frame_array(), Fs=synth.samplerate, noverlap=90, cmap=plot.cm.gist_heat)
    plot.show() 
Example 9
Project: GlosBio   Author: MikolajBalcerek   File: spectrogram_plot.py    MIT License 6 votes vote down vote up
def _plot_spectrogram_from_bytes(audio_bytes: bytes) -> plt.Figure:
    """
    Plots a spectrogram figure from an audio file (wav) under audio_path

    :param audio_bytes: bytes containing wav file audio
    :return: plt.Figure containing the spectogram
    """
    (rate, signal) = wav.read(audio_bytes)
    plt.clf()
    with catch_warnings():
        simplefilter("ignore")
        # TODO: gives a RuntimeWarning: divide by zero encountered in log10
        #  Z = 10. * np.log10(spec)
        plt.specgram(signal, Fs=rate)

    plt.ylabel('Frequency [Hz]')
    plt.xlabel('Time [sec]')
    plt.title("Spectrogram")
    figure = plt.gcf()
    return figure 
Example 10
Project: dagbldr   Author: dagbldr   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run_phase_reconstruction_example():
    fs, d = fetch_sample_speech_tapestry()
    # actually gives however many components you say! So double what .m file
    # says
    fftsize = 512
    step = 64
    X_s = np.abs(stft(d, fftsize=fftsize, step=step, real=False,
                      compute_onesided=False))
    X_t = iterate_invert_spectrogram(X_s, fftsize, step, verbose=True)

    """
    import matplotlib.pyplot as plt
    plt.specgram(d, cmap="gray")
    plt.savefig("1.png")
    plt.close()
    plt.imshow(X_s, cmap="gray")
    plt.savefig("2.png")
    plt.close()
    """

    wavfile.write("phase_original.wav", fs, soundsc(d))
    wavfile.write("phase_reconstruction.wav", fs, soundsc(X_t)) 
Example 11
Project: tfbldr   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run_phase_reconstruction_example():
    fs, d = fetch_sample_speech_tapestry()
    # actually gives however many components you say! So double what .m file
    # says
    fftsize = 512
    step = 64
    X_s = np.abs(stft(d, fftsize=fftsize, step=step, real=False,
                      compute_onesided=False))
    X_t = iterate_invert_spectrogram(X_s, fftsize, step, verbose=True)

    """
    import matplotlib.pyplot as plt
    plt.specgram(d, cmap="gray")
    plt.savefig("1.png")
    plt.close()
    plt.imshow(X_s, cmap="gray")
    plt.savefig("2.png")
    plt.close()
    """

    wavfile.write("phase_original.wav", fs, soundsc(d))
    wavfile.write("phase_reconstruction.wav", fs, soundsc(X_t)) 
Example 12
Project: spafe   Author: SuperKogito   File: vis.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def spectogram(sig, fs):
    """
    visualize a the spectogram of the given mono signal.

    Args:
        sig (array) : a mono audio signal (Nx1) from which to compute features.
        fs    (int) : the sampling frequency of the signal we are working with.
    """
    plt.specgram(sig, NFFT=1024, Fs=fs)
    plt.ylabel("Frequency (kHz)")
    plt.xlabel("Time (s)")
    plt.show(block=False)
    plt.close() 
Example 13
Project: tools   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def implot(arr, scale=None, title="", cmap="gray"):
    import matplotlib.pyplot as plt
    if scale is "specgram":
        # plotting part
        mag = 20. * np.log10(np.abs(arr))
        # Transpose so time is X axis, and invert y axis so
        # frequency is low at bottom
        mag = mag.T[::-1, :]
    else:
        mag = arr
    f, ax = plt.subplots()
    ax.matshow(mag, cmap=cmap)
    plt.axis("off")
    x1 = mag.shape[0]
    y1 = mag.shape[1]

    def autoaspect(x_range, y_range):
        """
        The aspect to make a plot square with ax.set_aspect in Matplotlib
        """
        mx = max(x_range, y_range)
        mn = min(x_range, y_range)
        if x_range <= y_range:
            return mx / float(mn)
        else:
            return mn / float(mx)
    asp = autoaspect(x1, y1)
    ax.set_aspect(asp)
    plt.title(title) 
Example 14
Project: irasl2018   Author: jfsantos   File: audio_tools.py    MIT License 5 votes vote down vote up
def implot(arr, scale=None, title="", cmap="gray"):
    import matplotlib.pyplot as plt
    if scale is "specgram":
        # plotting part
        mag = 20. * np.log10(np.abs(arr))
        # Transpose so time is X axis, and invert y axis so
        # frequency is low at bottom
        mag = mag.T[::-1, :]
    else:
        mag = arr
    f, ax = plt.subplots()
    ax.matshow(mag, cmap=cmap)
    plt.axis("off")
    x1 = mag.shape[0]
    y1 = mag.shape[1]

    def autoaspect(x_range, y_range):
        """
        The aspect to make a plot square with ax.set_aspect in Matplotlib
        """
        mx = max(x_range, y_range)
        mn = min(x_range, y_range)
        if x_range <= y_range:
            return mx / float(mn)
        else:
            return mn / float(mx)
    asp = autoaspect(x1, y1)
    ax.set_aspect(asp)
    plt.title(title) 
Example 15
Project: htm-challenge   Author: cloudbrain   File: plot_signal_mu.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def update_plot():
    global data, data2, b, a

    # plt.clf()
    # set the new data
    data_f = data
    # data_f = signal.lfilter(b1, a1, data_f)
    # data_f = signal.lfilter(b2, a2, data_f)
    data_f = data_f[-N_points:]
    # data_f = data
    li.set_ydata(data)
    li2.set_ydata(data2)

    ax.relim()
    ax.autoscale_view(True,True,True)
    fig.canvas.draw()


    # plt.clf()
    # spec = plt.specgram(data_f, NFFT=128, noverlap=32, Fs=250, detrend='mean', pad_to=256,
    #                     scale_by_freq=True)

    # # X = mne.time_frequency.stft(data, wsize)
    # # freqs = mne.time_frequency.stftfreq(wsize, sfreq=fs)

    # # imshow(np.log(abs(X[0])), aspect='auto',
    # #        origin='lower', interpolation="None",
    # #        vmin=-14, vmax=-4,
    # #        extent=[0, 4, 0, max(freqs)])
    # # ylim([0, 60])

    plt.draw()
    # # draw()


    # time.sleep(0.001)
    plt.pause(0.0001)                       #add this it will be OK. 
Example 16
Project: iqtool   Author: jgibbard   File: iqplot.py    The Unlicense 5 votes vote down vote up
def plotSpectrogram(data, fftWindow, fftSize, Fs):

    if fftSize == None:
        N = len(data)
    else:
        N = fftSize    
    
    if Fs == None:
        Fs = 2
    
    if fftWindow == "rectangular":
        plt.specgram(data, NFFT=N, Fs=Fs, 
        window=lambda data: data*np.ones(len(data)),  noverlap=int(N/10))
    elif fftWindow == "bartlett":
        plt.specgram(data, NFFT=N, Fs=Fs, 
        window=lambda data: data*np.bartlett(len(data)),  noverlap=int(N/10))
    elif args.fftWindow == "blackman":
        plt.specgram(data, NFFT=N, Fs=Fs, 
        window=lambda data: data*np.blackman(len(data)),  noverlap=int(N/10))
    elif fftWindow == "hamming":
        plt.specgram(data, NFFT=N, Fs=Fs, 
        window=lambda data: data*np.hamming(len(data)),  noverlap=int(N/10))
    elif fftWindow == "hanning":
         plt.specgram(data, NFFT=N, Fs=Fs, 
         window=lambda data: data*np.hanning(len(data)),  noverlap=int(N/10))

    plt.show() 
Example 17
Project: pyNAVIS   Author: jpdominguez   File: raw_audio_functions.py    GNU General Public License v3.0 5 votes vote down vote up
def wav_spectrogram(data, fs):
    plt.style.use('default')
    sptgrm_fig = plt.figure()

    sptgrm_fig.canvas.set_window_title('Spectrogram')
    plt.specgram(data,Fs=fs)
    plt.title('Spectrogram', fontsize='x-large')

    plt.xlabel('Time (ms)', fontsize='large')
    plt.ylabel('Frequency (Hz)', fontsize='large')
    colorbar = plt.colorbar()
    sptgrm_fig.show() 
Example 18
Project: fluctana   Author: minjunJchoi   File: fluctana.py    MIT License 5 votes vote down vote up
def spec(self, dnum=0, cnl=[0], nfft=512, **kwargs):
        if 'flimits' in kwargs: flimits = kwargs['flimits']*1000
        if 'xlimits' in kwargs: xlimits = kwargs['xlimits']

        fs = self.Dlist[dnum].fs
        nov = nfft*0.9

        for c in cnl:
            pshot = self.Dlist[dnum].shot
            pname = self.Dlist[dnum].clist[c]
            pbase = self.Dlist[dnum].time
            pdata = self.Dlist[dnum].data[c,:]

            pxx, freq, time, cax = plt.specgram(pdata, NFFT=nfft, Fs=fs, noverlap=nov,
                                                xextent=[pbase[0], pbase[-1]], cmap=CM)  # spectrum

            maxP = math.log(np.amax(pxx),10)*10
            minP = math.log(np.amin(pxx),10)*10
            dP = maxP - minP
            plt.clim([minP+dP*0.55, maxP])
            plt.colorbar(cax)

            if 'flimits' in kwargs:  # flimits
                plt.ylim([flimits[0], flimits[1]])
            if 'xlimits' in kwargs:  # xlimits
                plt.ylim([xlimits[0], xlimits[1]])
            else:
                plt.xlim([pbase[0], pbase[-1]])

            plt.title(pname, fontsize=10)  # labeling
            plt.xlabel('Time [s]')
            plt.ylabel('Frequency [Hz]')

            plt.show() 
Example 19
Project: pyroomacoustics   Author: LCAV   File: base.py    MIT License 5 votes vote down vote up
def plot(self, NFFT=512, noverlap=384, **kwargs):
        '''
        Plot the spectrogram of the audio sample. 

        It takes the same keyword arguments as
        `matplotlib.pyplot.specgram <https://matplotlib.org/api/_as_gen/matplotlib.pyplot.specgram.html>`_.
        '''

        import numpy as np
        try:
            import matplotlib.pyplot as plt
        except ImportError:
            print('Warning: matplotlib is required for plotting')
            return

        # Handle single channel case
        if self.data.ndim == 1:
            data = self.data[:,None]

        nchannels = data.shape[1]

        # Try to have a square looking plot
        pcols = int(np.ceil(np.sqrt(nchannels)))
        prows = int(np.ceil(nchannels / pcols))

        for c in range(nchannels):
            plt.specgram(data[:,c], NFFT=NFFT, Fs=self.fs, noverlap=noverlap, **kwargs)
            plt.xlabel('Time [s]')
            plt.ylabel('Frequency [Hz]')
            plt.title('Channel {}'.format(c+1)) 
Example 20
Project: AudioNet   Author: vishnu-ks   File: data_maker.py    MIT License 5 votes vote down vote up
def graph_spectrogram(wav_file):
    rate, data = get_wav_info(wav_file)
    print type(data), len(data)
    nfft = 256  # Length of the windowing segments
    fs = 256  # Sampling frequency
    pxx, freqs, bins, im = plt.specgram(data, nfft, fs)
    print "pxx : ", len(pxx)
    print "freqs : ", len(freqs)
    print "bins : ", len(bins)
    # plt.axis('on')
    # plt.show()
    plt.axis('off')
    print wav_file.split('.wav')[0]
    plt.savefig(wav_file.split('.wav')[0] + '.png',
                dpi=100,  # Dots per inch
                frameon='false',
                aspect='normal',
                bbox_inches='tight',
                pad_inches=0)  # Spectrogram saved as a .png
    try:
      im = Image.open(wav_file.split('.wav')[0] + '.png')
      rgb_im = im.convert('RGB')
      rgb_im.save(wav_file.split('.png')[0] + '.jpg')
    except Exception as e:
      print e
    if os.path.exists(wav_file.split('.wav')[0] + '.png'):
        os.system('convert '+(wav_file.split('.wav')[0] + '.png') + ' '+(wav_file.split('.wav')[0] + '.jpg'))
        os.remove(wav_file.split('.wav')[0] + '.png') 
Example 21
Project: DataHouse   Author: lucasxlu   File: music_util.py    MIT License 5 votes vote down vote up
def create_fft(filename):
    sample_rate, X = wavfile.read(filename)
    fft_features = abs(scipy.fft(X)[0:1000])
    base_fn, ext = os.path.splitext(filename)
    data_fn = FFT_NPY_DIR + base_fn.split('/')[-1] + '.fft'
    np.save(data_fn, fft_features)

    # draw the spec gram figure
    print(sample_rate, X.shape)
    # specgram(X, Fs=sample_rate, xextent=(0, 30)) 
Example 22
Project: dagbldr   Author: dagbldr   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def implot(arr, scale=None, title="", cmap="gray"):
    import matplotlib.pyplot as plt
    if scale is "specgram":
        # plotting part
        mag = 20. * np.log10(np.abs(arr))
        # Transpose so time is X axis, and invert y axis so
        # frequency is low at bottom
        mag = mag.T[::-1, :]
    else:
        mag = arr
    f, ax = plt.subplots()
    ax.matshow(mag, cmap=cmap)
    plt.axis("off")
    x1 = mag.shape[0]
    y1 = mag.shape[1]

    def autoaspect(x_range, y_range):
        """
        The aspect to make a plot square with ax.set_aspect in Matplotlib
        """
        mx = max(x_range, y_range)
        mn = min(x_range, y_range)
        if x_range <= y_range:
            return mx / float(mn)
        else:
            return mn / float(mx)
    asp = autoaspect(x1, y1)
    ax.set_aspect(asp)
    plt.title(title) 
Example 23
Project: tfbldr   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def implot(arr, scale=None, title="", cmap="gray"):
    import matplotlib.pyplot as plt
    if scale is "specgram":
        # plotting part
        mag = 20. * np.log10(np.abs(arr))
        # Transpose so time is X axis, and invert y axis so
        # frequency is low at bottom
        mag = mag.T[::-1, :]
    else:
        mag = arr
    f, ax = plt.subplots()
    ax.matshow(mag, cmap=cmap)
    plt.axis("off")
    x1 = mag.shape[0]
    y1 = mag.shape[1]

    def autoaspect(x_range, y_range):
        """
        The aspect to make a plot square with ax.set_aspect in Matplotlib
        """
        mx = max(x_range, y_range)
        mn = min(x_range, y_range)
        if x_range <= y_range:
            return mx / float(mn)
        else:
            return mn / float(mx)
    asp = autoaspect(x1, y1)
    ax.set_aspect(asp)
    plt.title(title) 
Example 24
Project: pyramic-dataset   Author: fakufaku   File: segment.py    MIT License 4 votes vote down vote up
def save_samples(filename):
    global pattern, output_dir, qc_images

    import re, os
    import numpy
    from scipy.io import wavfile
    from samplerate import resample

    from matplotlib import pyplot



    _, fname = os.path.split(filename)

    if not pattern.match(fname):
        return None

    spkr, angle = [int(i) for i in re.findall(r'\d+', fname)]
    signals = open_segment_rigid(filename, noise_thresh=3, off_lo=150, off_hi=150, plot=False)

    out_name = '{}_spkr{}_angle{}.{}'

    for name, signal in signals.items():

        if name == 'rate':
            continue

        if 'fq_sample' in name:
            # we resample the speech signals to 16kHz because this was the
            # orignal rate of the samples played 
            # this saves some storage and load speed is faster
            signal = resample(signal, 16000. / signals['rate'], 'sinc_best')
            rate = 16000
        else:
            rate = signals['rate']

        # save in folder with name of the sample
        folder = os.path.join(output_dir, name)
        if not os.path.exists(folder):
            os.mkdir(folder)

        # format filename with angle and speaker location
        filename = out_name.format(name, spkr, (360 - angle) % 360, 'wav')

        signal_float = signal.astype(numpy.int16)
        wavfile.write(os.path.join(folder, filename), rate, signal_float)

        if qc_images:
            # save a spectrogram for later inspection

            folder = os.path.join(output_dir, 'qc_images', name)
            if not os.path.exists(folder):
                os.mkdir(folder)

            filename = out_name.format(name, spkr, (360 - angle) % 360, 'png')

            pyplot.specgram(signal[:,0].astype(numpy.float32) / (2**15+1), Fs=rate, NFFT=1024, noverlap=512)
            pyplot.savefig(os.path.join(folder, filename))
            pyplot.clf() 
Example 25
Project: python-speech-enhancement   Author: chenwj1989   File: file_ns.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def main():
    input_file = "test/sp04_babble_sn10.wav"
    output_file = "test/out.wav"

    x, Srate = sf.read(input_file)

    # ====== set parameters ========
    interval = 0.02 #frame interval = 0.02s
    Slen = int(np.floor(interval * Srate))
    if Slen % 2 == 1:
        Slen = Slen + 1
    PERC = 50  #window overlap in percent of frame size
    len1 = int(np.floor(Slen * PERC / 100))
    len2 = int(Slen - len1)
    nFFT = 2 * Slen

    #Noise magnitude calculations - assuming that the first 6 frames is  noise/silence
    win = np.hanning(Slen)
    win = win * len2 / np.sum(win)
    noise_mean =np.zeros(nFFT)
    plt.specgram(x, NFFT=Slen, Fs=Srate, noverlap=len2, cmap='jet')
    plt.ylim((0, 4000))
    plt.show()

    j=1
    noise_frames = 6
    for m in range(1, noise_frames, 1):
        noise_mean = noise_mean + np.absolute(np.fft.fft(win * x[j:j + Slen], nFFT, axis=0))
        j=j + Slen
    noise_mu2 = (noise_mean / noise_frames) ** 2

    #=============================  User NoiseSuppression C=======================================================
    #initialize
    Nframes = int(np.floor(len(x) / len2) - np.floor(Slen / len2))
    xfinal = np.zeros(Nframes * len2)

    ns = NoiseSuppression('logmmse', 'vad', len2, Slen, nFFT)
    ns_parameters = ns.get_parameters()

    ns_parameters["noise_parameters"]['alpha'] = 0.98
    ns_parameters["spp_parameters"]['sap_priori_method'] = 'cohen'
    ns_parameters["snr_parameters"]['gamma_max'] = 40
    ns_parameters["snr_parameters"]['ksi_min'] = 10 ** (-25 / 10)
    ns_parameters["snr_parameters"]['dd_nr_snr'] = 0.98

    prev_ksi = np.ones(nFFT)
    ns.set_parameters(ns_parameters)
    ns.set_priori_noise(noise_mu2)
    ns.set_priori_snr(prev_ksi)

    #=============================  Start Processing =======================================================
    for k in range(0, Nframes*len2, len2):
        xfinal[k:k + len2] = ns.process_frame(x[k:k + len2])

    sf.write(output_file, xfinal, Srate)
    plt.figure(2)
    plt.specgram(xfinal, NFFT=Slen, Fs=Srate, noverlap=len2, cmap='jet')
    plt.ylim((0, 4000))
    plt.show() 
Example 26
Project: tools   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def run_phase_vq_example():
    def _pre(list_of_data):
        # Temporal window setting is crucial! - 512 seems OK for music, 256
        # fruit perhaps due to samplerates
        n_fft = 256
        step = 32
        f_r = np.vstack([np.abs(stft(dd, n_fft, step=step, real=False,
                                compute_onesided=False))
                         for dd in list_of_data])
        return f_r, n_fft, step

    def preprocess_train(list_of_data, random_state):
        f_r, n_fft, step = _pre(list_of_data)
        clusters = copy.deepcopy(f_r)
        return clusters

    def apply_preprocess(list_of_data, clusters):
        f_r, n_fft, step = _pre(list_of_data)
        f_clust = f_r
        # Nondeterministic ?
        memberships, distances = vq(f_clust, clusters)
        vq_r = clusters[memberships]
        d_k = iterate_invert_spectrogram(vq_r, n_fft, step, verbose=True)
        return d_k

    random_state = np.random.RandomState(1999)

    fs, d = fetch_sample_speech_fruit()
    d1 = d[::9]
    d2 = d[7::8][:5]
    # make sure d1 and d2 aren't the same!
    assert [len(di) for di in d1] != [len(di) for di in d2]

    clusters = preprocess_train(d1, random_state)
    fix_d1 = np.concatenate(d1)
    fix_d2 = np.concatenate(d2)
    vq_d2 = apply_preprocess(d2, clusters)

    wavfile.write("phase_train_no_agc.wav", fs, soundsc(fix_d1))
    wavfile.write("phase_vq_test_no_agc.wav", fs, soundsc(vq_d2))

    agc_d1, freq_d1, energy_d1 = time_attack_agc(fix_d1, fs, .5, 5)
    agc_d2, freq_d2, energy_d2 = time_attack_agc(fix_d2, fs, .5, 5)
    agc_vq_d2, freq_vq_d2, energy_vq_d2 = time_attack_agc(vq_d2, fs, .5, 5)

    """
    import matplotlib.pyplot as plt
    plt.specgram(agc_vq_d2, cmap="gray")
    #plt.title("Fake")
    plt.figure()
    plt.specgram(agc_d2, cmap="gray")
    #plt.title("Real")
    plt.show()
    """

    wavfile.write("phase_train_agc.wav", fs, soundsc(agc_d1))
    wavfile.write("phase_test_agc.wav", fs, soundsc(agc_d2))
    wavfile.write("phase_vq_test_agc.wav", fs, soundsc(agc_vq_d2)) 
Example 27
Project: irasl2018   Author: jfsantos   File: audio_tools.py    MIT License 4 votes vote down vote up
def run_phase_vq_example():
    def _pre(list_of_data):
        # Temporal window setting is crucial! - 512 seems OK for music, 256
        # fruit perhaps due to samplerates
        n_fft = 256
        step = 32
        f_r = np.vstack([np.abs(stft(dd, n_fft, step=step, real=False,
                                compute_onesided=False))
                         for dd in list_of_data])
        return f_r, n_fft, step

    def preprocess_train(list_of_data, random_state):
        f_r, n_fft, step = _pre(list_of_data)
        clusters = copy.deepcopy(f_r)
        return clusters

    def apply_preprocess(list_of_data, clusters):
        f_r, n_fft, step = _pre(list_of_data)
        f_clust = f_r
        # Nondeterministic ?
        memberships, distances = vq(f_clust, clusters)
        vq_r = clusters[memberships]
        d_k = iterate_invert_spectrogram(vq_r, n_fft, step, verbose=True)
        return d_k

    random_state = np.random.RandomState(1999)

    fs, d = fetch_sample_speech_fruit()
    d1 = d[::9]
    d2 = d[7::8][:5]
    # make sure d1 and d2 aren't the same!
    assert [len(di) for di in d1] != [len(di) for di in d2]

    clusters = preprocess_train(d1, random_state)
    fix_d1 = np.concatenate(d1)
    fix_d2 = np.concatenate(d2)
    vq_d2 = apply_preprocess(d2, clusters)

    wavfile.write("phase_train_no_agc.wav", fs, soundsc(fix_d1))
    wavfile.write("phase_vq_test_no_agc.wav", fs, soundsc(vq_d2))

    agc_d1, freq_d1, energy_d1 = time_attack_agc(fix_d1, fs, .5, 5)
    agc_d2, freq_d2, energy_d2 = time_attack_agc(fix_d2, fs, .5, 5)
    agc_vq_d2, freq_vq_d2, energy_vq_d2 = time_attack_agc(vq_d2, fs, .5, 5)

    """
    import matplotlib.pyplot as plt
    plt.specgram(agc_vq_d2, cmap="gray")
    #plt.title("Fake")
    plt.figure()
    plt.specgram(agc_d2, cmap="gray")
    #plt.title("Real")
    plt.show()
    """

    wavfile.write("phase_train_agc.wav", fs, soundsc(agc_d1))
    wavfile.write("phase_test_agc.wav", fs, soundsc(agc_d2))
    wavfile.write("phase_vq_test_agc.wav", fs, soundsc(agc_vq_d2)) 
Example 28
Project: synthesizer   Author: irmen   File: synth_demo.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def fm():
    synth = WaveSynth(samplerate=8000)
    from matplotlib import pyplot as plot
    freq = 2000
    lfo1 = Sine(1, amplitude=0.4, samplerate=synth.samplerate)
    s1 = synth.sine(freq, duration=3, fm_lfo=lfo1)
    plot.title("Spectrogram")
    plot.ylabel("Freq")
    plot.xlabel("Time")
    plot.specgram(s1.get_frame_array(), Fs=synth.samplerate, noverlap=90, cmap=plot.cm.gist_heat)
    plot.show()
    with Output(nchannels=1, mixing="sequential") as out:
        synth = WaveSynth()
        freq = 440
        lfo1 = Linear(5, samplerate=synth.samplerate)
        lfo1 = EnvelopeFilter(lfo1, 1, 0.5, 0.5, 0.5, 1)
        s1 = synth.sine(freq, duration=3, fm_lfo=lfo1)
        s_all = s1.copy()
        out.play_sample(s1)
        lfo1 = Sine(1, amplitude=0.2, samplerate=synth.samplerate)
        s1 = synth.sine(freq, duration=2, fm_lfo=lfo1)
        s_all.join(s1)
        out.play_sample(s1)
        lfo1 = Sine(freq/17, amplitude=0.5, samplerate=synth.samplerate)
        s1 = synth.sine(freq, duration=2, fm_lfo=lfo1)
        s_all.join(s1)
        out.play_sample(s1)
        lfo1 = Sine(freq/6, amplitude=0.5, samplerate=synth.samplerate)
        s1 = synth.sine(freq, duration=2, fm_lfo=lfo1)
        s_all.join(s1)
        out.play_sample(s1)
        lfo1 = Sine(1, amplitude=0.4, samplerate=synth.samplerate)
        s1 = synth.triangle(freq, duration=2, fm_lfo=lfo1)
        s_all.join(s1)
        out.play_sample(s1)
        freq = 440*2
        lfo1 = Sine(freq/80, amplitude=0.4, samplerate=synth.samplerate)
        s1 = synth.triangle(freq, duration=2, fm_lfo=lfo1)
        s_all.join(s1)
        out.play_sample(s1)
        # s_all.write_wav("fmtestall.wav")
        out.wait_all_played() 
Example 29
Project: dagbldr   Author: dagbldr   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def run_phase_vq_example():
    def _pre(list_of_data):
        # Temporal window setting is crucial! - 512 seems OK for music, 256
        # fruit perhaps due to samplerates
        n_fft = 256
        step = 32
        f_r = np.vstack([np.abs(stft(dd, n_fft, step=step, real=False,
                                compute_onesided=False))
                         for dd in list_of_data])
        return f_r, n_fft, step

    def preprocess_train(list_of_data, random_state):
        f_r, n_fft, step = _pre(list_of_data)
        clusters = copy.deepcopy(f_r)
        return clusters

    def apply_preprocess(list_of_data, clusters):
        f_r, n_fft, step = _pre(list_of_data)
        f_clust = f_r
        # Nondeterministic ?
        memberships, distances = vq(f_clust, clusters)
        vq_r = clusters[memberships]
        d_k = iterate_invert_spectrogram(vq_r, n_fft, step, verbose=True)
        return d_k

    random_state = np.random.RandomState(1999)

    fs, d = fetch_sample_speech_fruit()
    d1 = d[::9]
    d2 = d[7::8][:5]
    # make sure d1 and d2 aren't the same!
    assert [len(di) for di in d1] != [len(di) for di in d2]

    clusters = preprocess_train(d1, random_state)
    fix_d1 = np.concatenate(d1)
    fix_d2 = np.concatenate(d2)
    vq_d2 = apply_preprocess(d2, clusters)

    wavfile.write("phase_train_no_agc.wav", fs, soundsc(fix_d1))
    wavfile.write("phase_vq_test_no_agc.wav", fs, soundsc(vq_d2))

    agc_d1, freq_d1, energy_d1 = time_attack_agc(fix_d1, fs, .5, 5)
    agc_d2, freq_d2, energy_d2 = time_attack_agc(fix_d2, fs, .5, 5)
    agc_vq_d2, freq_vq_d2, energy_vq_d2 = time_attack_agc(vq_d2, fs, .5, 5)

    """
    import matplotlib.pyplot as plt
    plt.specgram(agc_vq_d2, cmap="gray")
    #plt.title("Fake")
    plt.figure()
    plt.specgram(agc_d2, cmap="gray")
    #plt.title("Real")
    plt.show()
    """

    wavfile.write("phase_train_agc.wav", fs, soundsc(agc_d1))
    wavfile.write("phase_test_agc.wav", fs, soundsc(agc_d2))
    wavfile.write("phase_vq_test_agc.wav", fs, soundsc(agc_vq_d2)) 
Example 30
Project: tfbldr   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def run_phase_vq_example():
    def _pre(list_of_data):
        # Temporal window setting is crucial! - 512 seems OK for music, 256
        # fruit perhaps due to samplerates
        n_fft = 256
        step = 32
        f_r = np.vstack([np.abs(stft(dd, fftsize=n_fft, step=step, real=False,
                                compute_onesided=False))
                         for dd in list_of_data])
        return f_r, n_fft, step

    def preprocess_train(list_of_data, random_state):
        f_r, n_fft, step = _pre(list_of_data)
        clusters = copy.deepcopy(f_r)
        return clusters

    def apply_preprocess(list_of_data, clusters):
        f_r, n_fft, step = _pre(list_of_data)
        f_clust = f_r
        # Nondeterministic ?
        memberships, distances = vq(f_clust, clusters)
        vq_r = clusters[memberships]
        d_k = iterate_invert_spectrogram(vq_r, n_fft, step, verbose=True)
        return d_k

    random_state = np.random.RandomState(1999)

    fs, d = fetch_sample_speech_fruit()
    d1 = d[::9]
    d2 = d[7::8][:5]
    # make sure d1 and d2 aren't the same!
    assert [len(di) for di in d1] != [len(di) for di in d2]

    clusters = preprocess_train(d1, random_state)
    fix_d1 = np.concatenate(d1)
    fix_d2 = np.concatenate(d2)
    vq_d2 = apply_preprocess(d2, clusters)

    wavfile.write("phase_train_no_agc.wav", fs, soundsc(fix_d1))
    wavfile.write("phase_vq_test_no_agc.wav", fs, soundsc(vq_d2))

    agc_d1, freq_d1, energy_d1 = time_attack_agc(fix_d1, fs, .5, 5)
    agc_d2, freq_d2, energy_d2 = time_attack_agc(fix_d2, fs, .5, 5)
    agc_vq_d2, freq_vq_d2, energy_vq_d2 = time_attack_agc(vq_d2, fs, .5, 5)

    """
    import matplotlib.pyplot as plt
    plt.specgram(agc_vq_d2, cmap="gray")
    #plt.title("Fake")
    plt.figure()
    plt.specgram(agc_d2, cmap="gray")
    #plt.title("Real")
    plt.show()
    """

    wavfile.write("phase_train_agc.wav", fs, soundsc(agc_d1))
    wavfile.write("phase_test_agc.wav", fs, soundsc(agc_d2))
    wavfile.write("phase_vq_test_agc.wav", fs, soundsc(agc_vq_d2))