1

My task is to load a signal, then take every 8th sample and compare it with 0.

If sample > 0 -> sample=1 else sample -> 0.

I have a code like this:

sn = wavread('example.wav',100);
z = sn(1 : 1 : end);
x = sn(1 : 1 : end);
for i = 1:rows(z);
  for j = 1:columns(z);
    if(z(i,j) < 0);
      z(i,j) = 0;
    else
    z(i,j) = 1;
    endif
  endfor
endfor
plot(x, "*",z, "o");

Result1

My problem is that if i select every 8th sample like this:

z = sn(1 : 8 : end);

It keep only every 8th sample and result is this:

Result2

What i need is to keep my 100 samples and print only every 8th so it keeps scale of first picture.

Thank's for any advice.

EDIT: I used

idx = 1:8:numel(z);
z(idx) = z(idx) > 0;

and now it looks like this:

result3

Is there any way to print out only samples that has value 1 or 0 and force them to stay on it's original indexes? For example on indexes 1,9,17,25,33 is val 1 or 0. I want them to stay on those indexes and ingnore others when i call plot(z) .

2 Answers2

2

For every point you have two coordinates, the value of the data

z = sn(1 : 1 : end);

and the position of the data

p=1 : 1 : 100;

so your plot(z) is equivalent to plot(p,z).
When you reduce/decimate the samples you need to mantain both the two coordinates

zd=z(1:8:end);
pd=p(1:8:end);
plot(pd,zd,"+")

As example

p=1:1:100;
z=sin(p*pi()/50);
subplot(1,2,1)
plot(p,z,"+")
pd=p(1:8:end);
zd=z(1:8:end);
subplot(1,2,2)
plot(pd,zd,"*")
print -djpg figure1.jpg

full data and decimate one

matzeri
  • 8,062
  • 2
  • 15
  • 16
  • +1 for figuring out what the OP really wants ;-) Am I wrong or is the comparison from the initial question completly irrelevant? – Andy Dec 14 '18 at 06:28
  • Basically he had two questions and I covered the less clear one as he was missing the hidden X,Y representation of data – matzeri Dec 14 '18 at 06:41
0

Don't use for loops, it's easy to vectorize this:

z = rand (100, 1);
idx = 1:8:numel(z);
z(idx) = z(idx) > 0;
Andy
  • 7,931
  • 4
  • 25
  • 45