How to write .wav file from data using wavio api - python

I'm using WAVIO python API to write data to a .wav file.
In order to test this api, First I read data .wav file and then I want to generate .wav containing the same data of the input .wav file, but I got an error while writing data to the output wav file.
import numpy as np
import wavio
wa = wavio.read("s24.wav") #Read a .wav file
print("x= "+str(wa.data)) #Data
print("rate= "+str(wa.rate)) #Rate
print("sampwidth= "+str(wa.sampwidth)) #sampwidth
wavio.write("sine24_output", wa.data, wa.rate,wa.sampwidth) #Error is here
Error is:
Traceback (most recent call last):
File "C:/Users/user/Desktop/Wav2Bytes/wavii.py", line 24, in <module>
wavio.write("sine24_output", wa.data, wa.rate,wa.sampwidth) #Write a .wav file. Error is here
File "C:\Program Files (x86)\Python\lib\site-packages\wavio.py", line 363, in write
vmin, vmax = scale
TypeError: cannot unpack non-iterable int object

You are missing the argument's name sampwidth. According to the docs, there are 5 arguments, if you don't specify the name sampwidth, it will instead set scale to wa.sampwidth. Anyway just use this:
wavio.write("sine24_output", wa.data, wa.rate,sampwidth=wa.sampwidth)

Related

Unknown file format while dealing with netcdf files from NASA (Python)

I'm attempting to gather temperature, humidity, and pm2.5 data for a project related to heat vulnerability. I'm using the following dataset from NASA to gather temperature data: MERRA-2 inst6_3d_ana_Nv: 3d,6-Hourly,Instantaneous,Model-Level,Analysis,Analyzed Meteorological Fields V5.12.4 (M2I6NVANA) https://disc.gsfc.nasa.gov/datasets/M2I6NVANA_5.12.4/summary
I've downloaded the .nc4 files using curl using these instructions: https://disc.gsfc.nasa.gov/data-access#mac_linux_curl
I'm using the netcdf4 python library to read and understand the data, but I'm being met with this error:
-bash-4.2$python pyScripts/nasaData.py
Traceback (most recent call last):
File "pyScripts/nasaData.py", line 5, in <module>
f = netCDF4.Dataset('/home/uja4bs/NASA_Data/MERRA2_400.inst6_3d_ana_Nv.20200101.nc4', 'r')
File "src/netCDF4/_netCDF4.pyx", line 2353, in netCDF4._netCDF4.Dataset.__init__
File "src/netCDF4/_netCDF4.pyx", line 1963, in netCDF4._netCDF4._ensure_nc_success
OSError: [Errno -51] NetCDF: Unknown file format: b'/home/uja4bs/NASA_Data/MERRA2_400.inst6_3d_ana_Nv.20200101.nc4'
Any pointers would be greatly appreciated!

Read s2p Z parameter files in Python

I am trying to read s2p files in using Python scikit-rf library. The s2p file in not in S parameter. I converted it to Z parameter. Looks like scikit-rf can read only S parameter file. Is there any other library or way to read s2p files with Z parameter. Below is how I am reading the file:
import skrf as rf
ntwk = rf.Network('ZTEST5_SHORTED.s10p')
s = ntwk.s
print(s)
Below is the error:
Traceback (most recent call last):
File "C:\Users\pujai\PycharmProjects\SIIntegration\practice.py", line 26, in <module>
ntwk = rf.Network('ZTEST5_SHORTED_DCfitted.s10p')
File "C:\Users\pujai\PycharmProjects\SIIntegration\venv\lib\site-packages\skrf\network.py", line 457, in __init__
self.read_touchstone(filename, self.encoding)
File "C:\Users\pujai\PycharmProjects\SIIntegration\venv\lib\site-packages\skrf\network.py", line 2050, in read_touchstone
raise NotImplementedError('only s-parameters supported for now.')
NotImplementedError: only s-parameters supported for now.
If I try to read an S parameter file, it works fine. Any help?

How to write a python function to compress a CSV file using LZ4

Newbie here...
I need to read and compress a CSV file using LZ4 and I have run into an expected error, the compress() function reads in bytes and the CSV file is incompatible. Is there a way to use LZ4 to compress an entire file or do I need to convert the CSV file into bit format and then compress it? If so how would I approach this?
import lz4.frame
import csv
file=open("raw_data_files/raw_1.csv")
type(file)
input_data=csv.reader(file)
compressed=lz4.frame.compress(input_data)
Error shows
Traceback (most recent call last):
File "compression.py", line 10, in <module>
compressed=lz4.frame.compress(input_data)
TypeError: a bytes-like object is required, not '_csv.reader'
You could do it like this:-
import lz4.frame
with open('raw_data_files/raw_1.csv', 'rb') as infile:
with open('raw_data_files/raw_1.lz4', 'wb') as outfile:
outfile.write(lz4.frame.compress(infile.read()))

Read differnet audio file formats from folder using Python

I am attempting to read different audio file formats from a folder using Python. The folder contains mainly .wav and .mp3 audio file formats. When I use the code listed below, I get the error:
Traceback (most recent call last): File
"G:/FAU/PythonCode/scipy_audio_spectrogram.py", line 7, in <module>
fs, Audiodata = wavfile.read(AudioName) File "C:\Python\lib\site-packages\scipy\io\wavfile.py", line 267, in read
file_size, is_big_endian = _read_riff_chunk(fid) File "C:\Python\lib\site-packages\scipy\io\wavfile.py", line 168, in
_read_riff_chunk
"understood.".format(repr(str1))) ValueError: File format b'ID3\x03'... not understood.
How to modify the code so that both .wav and .mp3 files can be read from the folder? Thanks!
Here is the code that I am using:
from scipy.io import wavfile # scipy library to read wav files
import numpy as np
AudioName = r'G:\FAU\PythonCode\4201.mp3' # Audio File
fs, Audiodata = wavfile.read(AudioName)

Error while reading wav file: wave.Error: unknown format: 6 [duplicate]

I try to open a wave file with the wave module, but I keep getting the same error whatever I try.
The line with the error is the following:
wav = wave.open(f)
This is the error message:
Traceback (most recent call last):
File "annotate.py", line 47, in <module>
play(file)
File "annotate.py", line 33, in play
wav = wave.open(f)
File "C:\Program Files (x86)\Python\lib\wave.py", line 498, in open
return Wave_read(f)
File "C:\Program Files (x86)\Python\lib\wave.py", line 163, in __init__
self.initfp(f)
File "C:\Program Files (x86)\Python\lib\wave.py", line 143, in initfp
self._read_fmt_chunk(chunk)
File "C:\Program Files (x86)\Python\lib\wave.py", line 269, in _read_fmt_chunk
raise Error('unknown format: %r' % (wFormatTag,))
wave.Error: unknown format: 49
String f is a path to a .WAV file and it works when played in any of my media players.
I have of course imported the wave module.
I tried f both as a relative and an absolute path.
I tried replacing "WAV" by "wav".
What is the error caused by?
Python's wave module works with a specific type of WAV: PCM (WAVE_FORMAT_PCM: 0x0001).
In your case, you're using a WAV of type WAVE_FORMAT_GSM610 [0x0031 = hex(49)].
You can use a program like Audacity or some lib for converting codecs to change the type of the WAV file.
You can see a list of WAV types here:
https://www.videolan.org/developers/vlc/doc/doxygen/html/vlc__codecs_8h.html
Python's wave module source code:
https://github.com/python/cpython/blob/master/Lib/wave.py
The file is compressed and the wave module does not support this type of compression.

Categories

Resources