Extract Numerator and Denominator of a vector (ratio)
5 次查看(过去 30 天)
显示 更早的评论
Hello Everyone,
I appreciate your helps in advance as I am a beginner in MATLAB!
I have a matrix and I should measure a ratio of a simple variable (it is a signal) and I need separate numerators and denominators (separated vectors) for next steps. My code is below. Thank you!
clc;
clear all;
close all;
%.....................................................................
data=xlsread('1');
amp=data(:,2);%us/f
time=data(:,1);%ms
t=time(1:1:end);% distans approximately 1 ms
am=amp(1:1:end);
l=length(t);
for i=1:l
if am(i)==-999.25
am(i)=am(i-1);
end
end
for j=2:l
r(j)=(am(j)-am(j-1))/(am(j)+am(j-1));
end
0 个评论
采纳的回答
Scott MacKenzie
2021-6-13
编辑:Scott MacKenzie
2021-6-13
It is not entirely clear what you are trying to do. Your 1st for-loop does nothing, since all the values are positive. However, if I understand correctly, your second for-loop reveals how you want to spit the signal into terms for the numerator (us) and denominator (f) terms. If that's correct, then, instead of a single vector r, you likely want separate vectors, us and f. Given this, here is what I put together:
data = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/651850/01.xlsx');
amp = data(:,2); % signal (us/f)
time = data(:,1); % time (1 ms intervals, not needed)
% this is equivalent to your second for-loop (except separating the terms)
us = amp(2:end) - amp(1:end-1);
f = amp(2:end) + amp(1:end-1);
tiledlayout(2,1);
nexttile;
plot(amp);
xlabel('Time (ms)');
ylabel('Signal');
nexttile;
p1 = plot(f);
hold on;
p2 = plot(us);
xlabel('Time (ms)');
ylabel('Signal (separated)');
legend({'f' 'us'}, 'location', 'east');
13 个评论
Scott MacKenzie
2021-6-14
us = amp(2:end) - amp(1:end-1);
f = amp(2:end) + amp(1:end-1);
r = us ./ f;
This calculation for r is equivalent to what you do in your second for-loop. I also added code to plot r using the stem function and I get the same plot as you, so that's good.
I'm still guessing what your overall goal is here. I think what you are trying to do is take the original signal (the vector amp in your code) and convert each value into a numerator term (us) and a denominator term (f). Is that correct? I suspect amp was actually calculated or measured that way in the first place and that you are trying to reverse engineer amp into the original data, us and f. These are what you call the upper layer and the lower layer. Is that correct?
So, if the formulas you provided for us and f are correct, then the plot for r should be the same as the plot for the original signal (amp), because that's what r is -- a recreation of the original signal from the original numerator and denominator terms. But, the two plots are different. That tells me that the formulas you provided for the numerator (us) and the denominator (f) are wrong. This raises the question, why are you calculating us and f like this:
us = amp(2:end) - amp(1:end-1);
f = amp(2:end) + amp(1:end-1);
This is my starting point -- trying to help with a MATLAB coding problem. But, it seems the fomulas might be wrong in the first place, and I'm not sure I can help with that.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!