Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Threshold a signal magnitude

Status
Not open for further replies.

yildiza

Newbie level 4
Joined
Nov 17, 2009
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
ISTANBUL
Activity points
1,318
Hi,

I simply wrote:

for i = 1 : NUMY;
if(Y(i)>50 || Y(i)<-50)
Y(i) = 0;
end
end

to limit magnitudes of array elements but it does not work. I am not familiar with matlab coding, anyone can help me about this simple problem? Thank you.
 

Hi yildiza,

First, using "i" as name for a variable is not very good in Matlab because Matlab uses "i" (and "j" too) for the imaginary unit. In your code you are redefining the meaning of "i", and in the case you put after a piece of code that uses "i" for the imaginary unit, the resul will be very different from expected.
In your code, to all the out-of-range values is assigned the value zero. Is that what you want?
Are you sure that the value of NUMY is the correct?
Why the result is not what you expected?
Regards

Z
 

    yildiza

    Points: 2
    Helpful Answer Positive Rating
Hi, again.

I want to limit magnitudes of my array's elements. So my threshold value is either +50 or -50.

My code part is:

[Y, FS, NBITS] = wavread('phnum1');
Y = Y * 2 ^ (NBITS-1);
NUMY = [0 : length(Y)-1];
figure;
subplot(1,2,1);
plot(NUMY,Y);
xlabel('sample index');
ylabel('signal');
subplot(1,2,2);
for k = 1 : NUMY
if( Y(k) > 50 )
Y(k) = 50;
elseif( Y(k) < -50 )
Y(k) = -50;
else
continue
end
end

plot(NUMY,Y);
xlabel('sample index');
ylabel('thresholded signal');
 

Hi yildiza

NUMY is defined as a vector but later it is used like a scalar.
Try this (changed lines are in red):

[Y, FS, NBITS] = wavread('phnum1');
Y = Y * 2 ^ (NBITS-1);
NUMY = [0 : length(Y)-1];
figure;
subplot(1,2,1);
plot(NUMY,Y);
xlabel('sample index');
ylabel('signal');
subplot(1,2,2);
for k = 1 : length(Y)
if( Y(k) > 50 )
Y(k) = 50;
elseif( Y(k) < -50 )
Y(k) = -50;
%else % this two lines can be removed
%continue % this two lines can be removed
end
end

plot(NUMY,Y);
xlabel('sample index');
ylabel('thresholded signal');

Regards

Z
 

    yildiza

    Points: 2
    Helpful Answer Positive Rating
try avoiding loops for matlab

hidx = find(x> 0.8);
loidx = find(x < 0.2);
x(hidx) = 0.8;
x(lodx) = 0.2

-b
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top