How to read a ".wav" file into MATLAB? - matlab

I wanted to input a wave file in the MATLAB so that I could process it using filters, when I come to input the wave file called wave.wav, this file is located on my desktop, and then I used
[y, fs, nb] = wavread('wave.wav');
to read the wave file but always gives me an error cannot open file, the only thing I can think of is that the function doesnt know the path of the wave.wav, any help?
And how can I play the file also using MATLAB after read, sound()?

Yes, you are correct on both counts. Use the full path to the file, and use the sound function to play it back. See this reference page for a thorough example. The documentation from the Mathworks is quite comprehensive.

This works:
[y,Fs]=wavread('filename');
sound(y,Fs);
note: the filename could be any audio file. but use a converter from .mp3 to .wav coz filename must be in wav format( few even say that waveread converts the file automatically into .wav file but in my case it did not!! )
:)

Use the full path to the file and you can play the sound using soundsc(y,fs) instead sound

Related

How to add unique image formats to Matlab's imread, and Matlab's drag and drop?

We have a number of internal image formats which I process in Matlab. I have read/write functions for all of them. For specificity, consider the TGA image format, for which there is a file exchange image reader.
Matlab has reasonable drag and drop support for image formats supported by imread.
That is, you can drag an image from explorer, drop it on the "Workspace" pane, and Matlab will read in the image, and copy it into your workspace.
I'd like to be able to add drag and drop support, and imread support for TGA files. (imread has nice autocomplete for filenames for instance, tga_read_image does not.)
I think this is what you are looking for. Quoting the official documentation:
open name opens the specified file or variable in the appropriate
application
You can extend the functionality of open by defining your own
file-handling function of the form openxxx, where xxx is a file
extension. For example, if you create a function openlog, then the
open function calls openlog to process any files with the .log
extension. The open function returns any single output defined by your
function.
For example:
function opentga(file)
% Your logic for reading and, eventually,
% displaying TGA files when drag and drop
% or other opening events occur.
end
And here is a full working example directly taken from the link:
function opentxt(filename)
[~, name, ext] = fileparts(filename);
fprintf('You have requested file: %s\n', [name ext]);
if exist(filename, 'file') == 2
fprintf('Opening in MATLAB Editor: %s\n', [name ext]);
edit(filename);
else
wh = which(filename);
if ~isempty(wh)
fprintf('Opening in MATLAB Editor: %s\n', wh);
edit(wh);
else
warning('MATLAB:fileNotFound', ...
'File was not found: %s', [name ext]);
end
end
end
An alternative path consists in overloading the uiopen function, as shown in this File Exchange release.
Starting off from Tommaso's answer, I created the following M-file on my MATLAB path:
function out = openics(filename)
img = readim(filename);
if nargout==1
out = img;
else
[~,varname] = fileparts(filename);
disp(['assigning into base: ',varname])
assignin('base',varname,img);
end
Dragging and dropping an ICS file onto MATLAB's command window shows the following on the command line:
>> uiopen('/Users/cris/newdip/examples/cermet.ics',1)
assigning into base: cermet
Check:
>> whos cermet
Name Size Bytes Class Attributes
cermet 256x256 65714 dip_image
Reading the code for uiopen (you can just type edit uiopen) shows that this calls open with the filename, which then calls openics with the filename and no output argument.
You can also type
img = open('/Users/cris/newdip/examples/cermet.ics');
to call openics and load the image into variable img.
NOTE 1: I'm using ICS because I don't have any TGA images to test with. ICS is a microscopy image file format.
NOTE 2: readim is a function in DIPimage
NOTE 3: This is cool, I had never bothered trying to drag and drop files onto MATLAB before. :)
The other answers address the "drag and drop" question. They do not address the question of how to integrate a proprietary image format into imread. This can be done fairly straight forwardly with the imformats command.
The issue of how/why it took me 3.5 years to figure that out will remain unanswered I'm afraid.... The feature has been around for 15+ years.

error in loading a file in MATLAB

I want to load a video in yuv extension using the function load.
load foreman_qcif.yuv
But I get this error
??? Error using ==> load
Unknown text on line number 1 of ASCII file C:\Users\Chaine\Downloads\foreman_qcif.yuv
"Øÿ".
Also, I would like to place the values or parameters of that video into a cell array. I found a function "struct2cell" but I think I used incorrectly. How must I manipulate the digital video in frames using matrices for each frame?
load is for loading .mat files (MATLAB data files) or ASCII files, see documentation for more details.
You probably want to use the VideoReader class, but you'll need to convert your file into a supported file format first.
I think there don't exist way to read (or, load) YUV files. Function load exists only for MAT files (.mat).
Check these links:
http://www.mathworks.com/matlabcentral/fileexchange/6318
https://www.mathworks.com/matlabcentral/fileexchange/36417-yuv-files-reading-and-converting

Splitting Audio Files with Matlab

How do you split an audio file into multiple parts using MATLAB ? Like I have an audio file composed of ten-twenty notes of a Piano, I need to split it into individual notes and store each note in a separate variable . Is it possible to do this with MATLAB ,if so how ? Can anyone please help me on this ?
If you want to do the splitting visually you can try "Simple Audio Editor" available in the file exchange.
http://www.mathworks.com/matlabcentral/fileexchange/19873-simple-audio-editor
I am the author of this program. Let me know if something does not work with that.
You can also try a free audio editor like audacity and export individual pieces to audio files. You can read each piece in MATLAB separately.
If you are looking to achieve this automatically you might need to ask this question in a Signal processing group.

How to programmatically combine .wav files?

I would like to play some kind of text-to-speech with only numbers. I can record 10 wav files, but how can I combine them programmatically ?
For instance, the user types 1234, and the text-to-speech combines 1.wav with 2.wav, 3.wav and 4.wav to produce 1234.wav that plays "one two three four".
1) create a new destination sample buffer (you will want to know the sizes).
2) read the samples (e.g. using AudioFile and ExtAudioFile APIs) and write them in sequence to the buffer. You may want to add silence between the files.
It will help if your files are all the same bit depth (the destination bit depth - 16 should be fine) and sample rate.
Alternatively, if you have fixed, known, sample rates and bit depths for all files, you could just save them as raw sample data and be done in much less time because you could simply append the data as is without writing all the extra audio file reading programs.
The open source project wavtools provides a good reference for this sort of work, if you're ok with perl. Otherwise there is a similar question with some java examples.
The simplist common .wav (RIFF) file format just has a 44 byte header in front of raw PCM samples. So, for these simple types of .wav files, you could just try reading the files as raw bytes, removing the 44 byte header from all but the first file, and concatening the samples. Or just play the concatenated samples directly using the Audio Queue API.

copy part of audio file into a new file in iOS

I want to copy a part of the audio file, given the starting and ending point(in terms of time which I'll convert to packets or frames-is the conversion right?), and create a new audio file for the copied snippet. How do I copy?
Please advice.
Regards,
Namratha
For what file format(s)?
For the simplest and common .WAV (RIFF) file format, you can just copy the canonical 44-byte header (after checking to make sure the file is using only this simple format), update with the target file length, and then copy a selected sub-range of bytes (multiply time by sample rate by frame size) from the source file, and append that PCM data to the copied header. Apple's codec does not complain about audio files patched together this way.
For other formats, you might be able to convert them either to a simple WAVE file, or to an array of raw PCM samples of suitable sample rate, data type and endianess, and then do the above.