<jupyter_start><jupyter_text>In this notebook we will use image compression technique to compress audio signals.The audio files in this notebook are from the ESC-50 dataset:    - ESC-50: Dataset for Environmental Sound Classification    - https://github.com/karoldvl/ESC-50/    - https://dx.doi.org/10.7910/DVN/YDEPUTA good application of svd and a demo for images is here:http://timbaumann.info/svd-image-compression-demo/The outline is as follows:- Show a function that compresses an image (with a matrix based technique called svd)- Load<jupyter_code>%matplotlib inline
import numpy as np<jupyter_output><empty_output><jupyter_text>Define plotting helper functions<jupyter_code>from librosa import display

def plot_spectrum (data, name):
    display.specshow(data, y_axis='log', x_axis='time')
    plt.title(f'Power spectrogram of {name}')
    plt.colorbar(format='%+2.0f dB')
    plt.tight_layout()
    # plt.show()<jupyter_output><empty_output><jupyter_text>Define compression helper functions<jupyter_code>random_seed = 0
from sklearn.decomposition import TruncatedSVD
np.random.seed(random_seed)

def compress(imageIn, n_components=100,random_seed=0):
    
    image = imageIn
    
    if len (image.shape) == 2:
        # print('Found Gray image')
        image = image[:,:,np.newaxis] # Equivalent to x[:,np.newaxis]
    
    if len (image.shape) == 3:
        # print('Found RGB image')
        pass
        
    if len (image.shape) > 3:
        raise('not sure what image type this')
        
    n_of_layers = image.shape[2]
    
    compressed_list = []
    
    for layer in range(n_of_layers):
        # print(layer)
        image_layer = image[:,:,layer] # ie r, g or b
        
        clf = TruncatedSVD(n_components=n_components)
        clf.fit(image_layer)
        compressed_layer = clf.inverse_transform(clf.transform(image_layer))
        
        compressed_list.append(compressed_layer)
        
        
    compressed = np.stack(compressed_list, axis=2)
    
    
    
    # clip to expected range
    compressed = np.clip(compressed, a_min=0, a_max=255)
    
    # cast to same dtype as original image
    compressed = np.array(compressed, dtype = image.dtype)
    
    # reshape to original image size
    compressed = compressed.reshape(imageIn.shape)
    
    return compressed
def compress_complex(complex_image, n_components=100,random_seed=0, doplot=False):
    '''
    complex in the sense of complex numbers,
    https://en.wikipedia.org/wiki/Complex_number
    
    ie having a real and an imaginary part
    
    '''
    real = np.real(complex_image)
    imag = np.imag(complex_image)
    
    compress_real = compress(real, n_components, random_seed)
    compress_imag = compress(real, n_components, random_seed)
    
    if doplot:
        plot_spectrum(real, 'Real Part')
        plot_spectrum(compress_real, 'Real Part Compressed')
        
        plot_spectrum(imag, 'Imaginary Part')
        plot_spectrum(compress_imag, 'Imaginary Part Compressed')
    
    compressed = compress_real + 1j * compress_imag
    return compressed<jupyter_output><empty_output><jupyter_text>Compress Images<jupyter_code>from sklearn.datasets import load_sample_images
dataset = load_sample_images()
dataset.DESCR
def myplot(compressed_image):
    plt.imshow(compressed_image)
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
import matplotlib.pyplot as plt

@interact(ImageNumber=range(len(dataset.images)), 
          n_components=widgets.IntSlider(min=1, max=70, step=1, value=10))
def plot_compressed(ImageNumber,n_components):
    image = dataset.images[ImageNumber]
    compressed_image = compress(image, n_components=n_components)
    fig = plt.figure(figsize=(15, 7))
    myplot(compressed_image)
    fig.canvas.draw()<jupyter_output><empty_output><jupyter_text>SoundsThe audio files in this notebook are from the ESC-50 dataset:- ESC-50: Dataset for Environmental Sound Classification- https://github.com/karoldvl/ESC-50/- https://dx.doi.org/10.7910/DVN/YDEPUT<jupyter_code>def load_audio_file(filepath):
    # %%
    y_multichannel, sr = lr.load(filepath, mono=False)
    print(y_multichannel.shape)

    if len(y_multichannel.shape)>1:
        channels = [0]
        y_channel_selection = y_multichannel[tuple([channels])]

        y = np.mean(y_channel_selection, axis=0)
    else:
        y = np.array(y_multichannel)
    
    return y, sr
# %%
import pathlib
import IPython
import librosa as lr
from glob import glob
import numpy as np
import matplotlib.pyplot as plt



# files = glob(pathname='data/hitachi/0_db/slider/id_00/**/**.wav')
files = glob(pathname='sounds/**/**.wav')
print(files)
filepath = files[0]

# %%
image_dict = {'Cat 1': files[1], 'Cat 2': files[2], 'Can opening':files[0]}
@interact(x=image_dict,
          plt=plt.figure(figsize=(15, 7))
         )
def myprint(x):
    print(x)
    y, sr = load_audio_file(x)
    IPython.display.display(IPython.display.Audio(y, rate=sr))
    
    mysftf = lr.stft(y, n_fft= 1024, hop_length= 512)
    
    fig=plt.figure(figsize=(15, 7))
    plot_spectrum(np.log(np.abs(mysftf)), 'Log of Absolute of Compressed')
    fig.canvas.draw()

@interact(x=image_dict,
          n_components=widgets.IntSlider(min=1, max=70, step=1, value=25))
def myprint(x, n_components):
    print(x)
    y, sr = load_audio_file(x)
    # IPython.display.display(IPython.display.Audio(y, rate=sr))
    mysftf = lr.stft(y, n_fft= 1024, hop_length= 512)
    # plot_spectrum(np.abs(mysftf), 'Absolute of Uncompressed')
    # 
    
    mysftf_compressed = compress_complex(mysftf, n_components=n_components, doplot=False)
    
    plt.figure(figsize=(15, 7))
    plot_spectrum(np.log(np.abs(mysftf_compressed)), 'Log of Absolute of Compressed')

    Original_memory = np.prod(mysftf_compressed.shape)
    Compressed_memory = (1 + np.sum(mysftf_compressed.shape) ) * n_components
    print(f'The compressed memory is roughly {100 * 2 * Compressed_memory / Original_memory:0.0f}% of the original')
    y_inverted_sftf = lr.istft(mysftf_compressed, hop_length= 512)
    IPython.display.display(IPython.display.Audio(y_inverted_sftf, rate=22050))
    print('\n')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11141347846966544, 'fraction_numerical': 0.017608452056987352, 'mean_word_length': 3.773109243697479, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '501719', 'license': 'MIT', 'n_tokens_mistral': 2216, 'n_tokens_neox': 2107, 'n_words': 445, 'path': '.ipynb_checkpoints/Compress_audio_via_images-checkpoint.ipynb', 'repo_name': 'hfwittmann/sound', 'size': '6247'}