# Deleting a signal by time segments

Discussion in 'Programmer's Corner' started by battlehands, Jul 29, 2011.

1. ### battlehands Thread Starter New Member

Jul 29, 2011
13
0
I have read a ~40 second speech signal into MATLAB. I need to delete this signal by 8 seconds and add zeros to the last segment to make it 8 seconds long. How do I do this?

2. ### guitarguy12387 Active Member

Apr 10, 2008
359
12
Code ( (Unknown Language)):
1. sound_del = sound(1:(length(sound) - 8*Fs)); % gets rid of last 8 seconds
2. sound_padded = [sound_del zeros(1, 8*Fs)]; % Adds 8 seconds of zeros
Where Fs is your sampling rate

3. ### battlehands Thread Starter New Member

Jul 29, 2011
13
0
I believe deleting the signal by 8 seconds means to break the signal into sections of length 8 seconds.

I would like to pad the last 8 seconds with zeros instead of deleting them.

For example,

There maybe be 3 seconds of valuable information in the last 8 second interval, so 5 seconds of zeros would be desirable.

For my signal, I dont yet know how many seconds of valuable information there will be in the last 8 second interval.

4. ### guitarguy12387 Active Member

Apr 10, 2008
359
12
Whaaaa?

I am thoroughly confused.

Regardless, the approach and code is still valid. Use the 'colon' notation and concatenation of vectors. You can get the limits by thinking about how your sampling rate relates to time. You can even just use the above code and modify it slightly to save, delete, or zero pad any section you want. Experiment!

5. ### battlehands Thread Starter New Member

Jul 29, 2011
13
0
It was my understanding that deleting the signal meant breaking the signal up into segments. So to delete the signal by 8 seconds meant to section it off into 8 second segments. Since the amount of time of the signal leaves the last section with sub 8 seconds of information, I need to pad it with zeros. Sorry for the confusion.

6. ### guitarguy12387 Active Member

Apr 10, 2008
359
12
Hey! Well maybe i'm ignorant, but i've never heard that before! Thats okay, I understand a bit better now (though, admittedly, not fully....).

So if you dissect my code, you will see two main mablab principles that you can use to construct your own code to fit your design parameters.

1) The usage of the : (colon) notation. As you may or may not know, matlab is based around matrices (Matlab = MATrix LABratory). In this environment, we think of signals as being stuffed into a one-dimensional vector. In your case, the vector is indexed by the sample number, and the value of the vector at a given index is equal to the amplitude for a given sample. Make sense?

So what?! Well, using the colon notation, you can select a range in a vector. y=x(1:10) is synonymous with saying, "the new vector y is assigned the first ten samples of the previously defined vector x." In this way, you can sort of 'partition' your input signal and break it up into segments of differing lengths.

2) The notion of sampling. When your signal was recorded onto a computer, it was sampled at some rate. When you import that signal into matlab, it assumes (or is explicitly told) what the sampling rate is. For typical speech signals on a cd, this is 44.1KHz (although other standards exist). What does this mean for your question? Well by using the sampling rate information you can determine how many samples are necessary to make up 1 second worth of data. Note that saying a signal is sampled at 44.1KHz is more accurately described as 44100 Samples Per second.

Combining these two thoughts, if you want to assign the first second of the signal 'x' to the signal 'y', the syntax would be: y = x(1:Fs);
Where Fs is your sampling rate. By specifying that you want 44,100 samples is coincidental with the fact that there is 44,100 samples in one second. If you want 2 seconds, y = x(1:2*Fs)... 8 seconds, y = x(8*Fs) and so on.

Makes sense?

As a side note, the function 'zeros(r, c)' creates a matrix of zeros with rows 'r' and columns 'c'. For example, zeros(1, 5) = [0 0 0 0 0]

This can be used to concatenate with the parts of your signal to get your zero padding.

You can use matlab's help pages for more information. In a matlab command, type 'help zeros'.

This 'help' command can be used for any matlab function