Tuesday, March 15, 2022

Python: Fast Fourier Transform (FFT) for speech processing using Scipy

Scipy provides the FFT and inverse FFT functions useful for sound processing.

Example:

import soundfile as sf

from scipy.fftpack import fft, ifft

with open('1.wav', 'rb') as f_wav:

    x, rate = sf.read(f_wav)

   

X = fft(x)

#The ifft function returns a complex variable, but the imaginary part is quite small to be neglected.

y_complex = ifft(X)

y = y_complex.real

sf.write('2.wav', y, 16000)


Reference:

Matlab: Discrete Cosine Transform (DCT) for speech processing (StudyEECC)

Python: Discrete Cosine Transform (DCT) for speech processing (StudyRaspberryPi)

Fourier Transforms (scipy.fft)

scipy and numpy inverse fft returns complex numbers not floats, can't save as wav (StackOverflow)

Python - How to use Soundfile to read and write WAV and FLAC files (Study Raspberry Pi)

Wednesday, March 9, 2022

Python: Discrete Cosine Transform (DCT) for speech processing using Scipy

The DCT and inverse DCT may be used to convert a speech signal into the transform domain using real values and back to the speech waveform.

Example using Python and Scipy:

import soundfile as sf

from scipy.fftpack import dct, idct


#DCT and IDCT functions have to be normalized.

def dct_norm (block):

    return dct(block.T, norm = 'ortho').T

def idct_norm (block):

    return idct(block.T, norm = 'ortho').T

  

with open('1.wav', 'rb') as f_wav:

    x, rate = sf.read(f_wav)


X = dct_norm(x)

y = idct_norm(X)


sf.write('2.wav', y, 16000)


Note that the DCT and IDCT functions have to be normalized to obtain identical results to MATLAB.

Reference:

Matlab: Discrete Cosine Transform (DCT) for speech processing (StudyEECC)

scipy.fftpack.dct

Discrete Cosine Transforms (Scipy Tutorial)

In scipy why doesn't idct(dct(a)) equal to a? (StackOverflow)

Python - How to use Soundfile to read and write WAV and FLAC files (Study Raspberry Pi)

Tuesday, March 8, 2022

Python - How to use Soundfile to read and write WAV and FLAC files

WAV and FLAC are two commonly used sound file formats:

WAV - Waveform Audio File Format (Wikipedia)

FLAC - Free Lossless Audio Codec (Wikipedia)

The major difference between the two formats is that WAV is without compression and FLAC is for lossess compression.

In Python, to read and write WAV and FLAC files including export in a different format, simply use the code with Soundfile as below:

import soundfile as sf

with open('wav/1.wav', 'rb') as f_wav:

    x_wav, rate = sf.read(f_wav)

with open('flac/2.flac', 'rb') as f_flac:

    x_flac, rate = sf.read(f_flac)

#exchange sound formats by saving

sf.write('1.flac', x_wav, 16000)

sf.write('2.wav', x_flac, 16000)

Tuesday, January 5, 2021

Python - Create an empty array and apply maximum/minimum clipping limits to a vector

 The code below shows how to

1. Define array a as type = float 32. (aka Single in Matlab)

2. Create empty array of zeros b as the same shape and type as a

3. Apply min/max limits to a and the clipped results to b

4. Make a copy of as c and apply the same min/max clipping limits.

import numpy as np

#Array of float32. Without dtype, the default is float64.

a = np.array([0.21, 0.92, 0.89, 1.67, -0.12],dtype=np.float32)


#Create b with the same dimension as a

b = np.zeros(a.shape,dtype=a.dtype)


#clip

np.clip(a,0,1,out=b);


#make a copy of a as c. If use = c, the content of will change with c.

c = a.copy()

c[c>1]=1

c[c<0]=0

Variable explorer results with Spyder:



Reference:

Single-precision floating-point (Wikipedia)

Monday, October 12, 2020

Python and Openpyxl - Read in multiple txt files and store as different worksheets in a single Excel file

If you have some text file and want to combine them into different worksheets of the same Excel file, simply follow the example below using Python:

1. Create some txt files and separate data with tabs:


2. Save the following code as a python file:

import csv
import openpyxl

inputArray = ['a', 'b', 'c']

wb = openpyxl.Workbook()
output_file = 'output.xlsx'

#loop in the reversed order so that the first file is stored as the first sheet.
for item in reversed(inputArray):

    input_file = item+'.txt'
    sheet = wb.create_sheet(item, 0)

    with open(input_file, 'rt') as data:
    reader = csv.reader(data, delimiter='\t')
    for row in reader:
    sheet.append(row)

wb.save(output_file)

Note: install openpyxl using pip or miniconda if required.

3. result:



Reference:

如何使用 Python 程式操作 Excel 試算表 (TechBridge 技術共筆部落格 by kdchang)

Friday, October 9, 2020

Raspberry - Pronunciation 什麼!? 樹莓派的英文發音竟然跟你想的不一樣?

你接觸樹莓派(Raspberry Pi)多久了呢?

「樹莓」的英文字Raspberry看起來不難發音吧... 你也是唸Rasp+Berry嗎?

其實,跟想像的不一樣喔!竟然是:


p不發音!p不發音!p不發音!(很重要所以說三次!)


所以唸起來比較像是Raz-berry

記起來了嗎?


發音聽這邊:

How to say raspberry (Cambridge Dictionary YouTube) (包括英式與美式發音)

這30個台灣人常唸錯的英文字😲 你答對幾個? (Lily Chen YouTube 6:00)

Tuesday, March 8, 2016

JavaScript and JSON

If we have a JSON string like this:

{"result":{"information":[{"name":"Tom","age":25},{"name":"Andy","age":30},{"name":"Peter","age":35},{"name":"David","age":40}]}}

How do we use it in JavaScript?

The answer is very easy. Just use JSON.parse().

JavaScript code:


<script language="JavaScript">
<!--

var JSONStr ='{"result":{"information":[{"name":"Tom","age":25},{"name":"Andy","age":30},{"name":"Peter","age":35},{"name":"David","age":40}]}}';
var JSONObj = JSON.parse(JSONStr);

var count;
for (count = 0 ; count <=3 ; count++){
    document.write("<br><br>");
    document.write(JSONObj.result.information[count].name+" ");
    document.write(JSONObj.result.information[count].age);
}
</script><noscript>
Your browser doesn't support JavaScript.
</noscript>

Result:


Related Information:

Simple JavaScript code - Hello World and Current Time
iOS: Print out JSON content in Swift playground