Add 1 second of silence to the .wav sound file - matlab

I need to create a function in MATLAB, but I'm new to the program so I would appreciate any help from you guys. I've read the other posts here, but unfortunately none have helped me.
Here's the question: How can I add 1 second of silence to both the beginning and the ending of a WAV file with MATLAB?
My current code:
subplot(2,1,1)
[s1,Fs]=wavread('sound.wav');
t=linspace(0,1,Fs);
plot(t,s1)
Thanks!

Step 1: Create a vector of zeros. (size of vector will depend on sampling rate).
silence = zeros(Fs, 1);
finalWav = [silence s1 silence];
Step 2: Concatenate the vectors.
That should do it.
Please note, that I do not have access to Matlab and am not able to test the code.

Related

Matlab read multiples samples with audioread

I'm trying to read multiple samples from a mp3 file with the function audioread.
let's say I have the following code :
samples = [1, 44100; 50000, 100000; 150000, 200000];
[signal, fe] = audioread(filepath, samples);
t = (1/fe:1/fe:lenght(signal)/fe);
signal = signal(:,1);
How can I make this work ? If samples contain only 1 row, it works just fine, but I have like 80 samples in the same mp3 file to work with.
I've tried this with a for loop, the audioread part works fine, but I don't know how to store the data since the duration of each sample isn't the same, the dimensions of matrices are not consistent and I can't concatenate them in a single matrix.
Plus, I've been told for loops are better avoided whenever possible in Matlab, so I'd like to do this by the rules (but i'll use them if there's no other way ofc).
Finally I'm very new to Matlab so I might miss something very obvious, but cound'nt find out reading through the doc.
Thks for your help :)

Using repmat() to equalise the length of two audio files Matlab

Having a slight issue with the repmat() function.
I've got two audio files that I'm using for cross synthesis in Matlab, I want to make the second audio file repeat so it's the same length as the first one.
Currently I'm doing
c = size(y());
rep = y2(1:end,:);
y(1:end,:) = repmat(rep,1, c(1))
Yet it's not making a 1D matrix, any suggestions?
y and y2 are just the y values of each song converted to mono.
Thanks!
I actually managed to solve it myself, turns out I was using them in the wrong order, needed to be repmat(rep c(1), 1)!

Input 2 .wav audio files

I need to have 2 .wav files reading and playing through a delay based function so that I can apply the effect to both signals at the same time and it seems like it should be easy, but I'm an amateur and could do with a bit of help.
My idea was just to enhance the original signal with a slightly out of phase version but I can't seem to work it out.
Is it possible to do this?
So far:
[Input, FS] = audioread('input_audio.wav');
but is there someway i can put in two? I've tried with commas, spaces, semi-colans, etc such as like this...
[Input, FS] = audioread('Vln1_D4_open.wav':'Vln1_room_D4_open.wav');
but this doesn't work.
the next part of the function is:
% First, to make sure the audio is single channel
[LEN, channels] = size(Input);
if (channels == 2) Input = Input(:,1); end;
so do I need to change this part instead?
Thank you in advance to anyone who can help! =)

Splitting an audio file in Matlab

I'm trying to split an audio file into 30 millisecond disjoint intervals using Matlab. I have the following code at the moment:
clear all
close all
% load the audio file and get its sampling rate
[y, fs] = audioread('JFK_ES156.wav');
for m = 1 : 6000
[t(m), fs] = audioread('JFK_ES156.wav', [(m*(0.03)*fs) ((m+1)*(0.03)*fs)]);
end
But the problem is that I get the following error:
In an assignment A(I) = B, the number of elements in B and I
must be the same.
Error in splitting (line 12)
[t(m), fs] = audioread('JFK_ES156.wav', [(m*(0.03)*fs)
((m+1)*(0.03)*fs)]);
I don't see why there's a mismatch in the number of elements in B and I and how to solve this. How can I get past this error? Or is there just an easier way to split the audio file (maybe another function I don't know about or something)?
I think the easiest way to split audio is to just load it and use the vec2mat function. so you would have something like this;
[X,Fs] = audioread('JFK_ES156.wav');
%Calculate how many samples you need to capture 30ms of audio
matSize = Fs*0.3;
%Pay attention to that apostrophe. Makes sure samples are stored in columns
%rather than rows.
output = vec2mat(x,matSize)';
%You can now have your audio split up into the different columns of your matrix.
%You can call them by using the column calling command for matrices.
%Plot first 30ms of audio
plot(output(:,1));
%You can join the audio back together using this command.
output = output(:);
Hope that helps. Another good thing about this method is that it keeps all your data in one place!
Edit : One thing I thought of, you may get a problem with this depending on your vector size. But I think vec2mat actually zeroPads your vector. Not a big thing, but if you're moving back and forth between the two, then it might be a good idea to have another variable that stores the original length of your signal.
You should just use the variable y and reshape it to form your split audio. For example,
chunk_size = fs*0.03;
y_chunks = reshape(y, chunk_size, 6000);
That will give you a matrix with each column a 30 ms chunk. This code will also be faster than reading small segments from file in a loop.
As hiandbaii suggested you could also use cell array. Make sure you clear your existing variables before that. Not clearing the array t is probably the reason you got the error "Cell contents assignment to a non-cell array object."
Your original error is because you cannot assign a vector with scalar indexing. That is, 'm' is a scalar, but your audioread call is returning a vector. This is what the error says about mismatch in size of I and B. You could also fix that by making t a 2-D array and use an assignment like
[t(m,:), fs] =
It appears that each 30 ms segment is not equal to one sample. That would be the only case where your code works. i.e. 0.03*fs != 1.
You could try using cells instead.. i.e. replace t(m) with t{m}

fft on samples of an audio file in matlab

I'm trying to extract information from a sound file in order to use it in a video classification algorithm I'm working on.
My problem is that I don't know how to work exactly with audio files in Matlab.
Below is what I need to accomplish:
open the audio file and get the sampling rate/frequency
I need to work on a window of 2 seconds so I have to loop over the file and get each 2 seconds as a window and then do the ftt (Fast-Fourier-Transform) on each window.
After that it is my turn to use these values to do what I want
any help would be appreciated
Thanks.
Following code may only give you some idea. You may need to determine another fft size, a windowing function like hamming etc.
To read a wav file:
[data, Fs] = wavread('path.wav');
Splitting to 2 sec windows and getting fft:
frameFFT = [];
timeStep = Fs*2;
for i=1:timeStep:length(data)-timeStep
frameFFT = [frameFFT; fft(data(i:i+timeStep-1),1024)];
end