Shift data in the negative x direction then multiply shifted data with other plot

Hello,
I have two plots from a simulated loop. I wanted to shift the data in plot 2 in the negative x direction, then multiply the shifted data with plot 1. How can I go about doing that? I have attached my data file.
Here is the code that I have so far:
% Read the file and store into matrix v
v=csvread('data_1.csv');
% Frequency vector
freq1 = v(:,1);
plot1 = v(:,2);
freq2 = v(:,3);
plot2 = v(:,4);
freq2_shifted = freq2*.5;
semilogx(freq1,plot1);
ylim([-20 10]);
grid on;
hold on
semilogx(freq2,plot2);
semilogx(freq2_shifted,plot2);
legend('plot 1','plot 2','shifted');
hold off

댓글 수: 2

I actually wanted the shifted plot to look like that below. With the blue plot being shifted to the green plot's position, then multiplying the green plot and red plot together to get the "Product" plot. I was not sure how to approach this issue and led you down the wrong path of implementing the circshift function. Sorry and Thanks for your help.
No worries.
See my Comment.

댓글을 달려면 로그인하십시오.

 채택된 답변

Star Strider
Star Strider 2016년 2월 18일
The circshift function would be my approach, but I don’t know what you intend by ‘shift’.

댓글 수: 9

Hello, thanks for your solution. I wanted to shift the frequency (or x-axis) by 0.01. Then find the product of this shifted data and plot 1. Thanks.
I still don’t know what you want.
Do you want to shift it circularly, or something else? If you don’t shift it circularly, and just delete one in ‘freq2’ and ‘plot2’ on the left, you have to shorten ‘freq1’ and ‘plot1’ as well for them to both be equal in length. What do you want to do?
My apologies. I wanted to shift it circularly.
The just use circshift. Shifting freq2 and data2 ‘left’ by about 0.01 involves shifting them by one position up (since they’re column vectors):
freq2s = circshift(freq2, [-1 0]);
data2s = circshift(data2, [-1 0]);
Your frequencies are greater than those in the image you posted by an order of magnitude. Other than that, I see no reason that this would not be the result you want:
prd = plot1 .* plot2;
figure(2)
semilogx(freq2, prd, '--')
grid
It looks to me that (except for the frequencies being an order of magnitude higher) it reproduces the plot you posted. Tuck that semilogx (or just plot) call into your code before the legend call, add ‘Product’ to your legend strings, and the result should be what you want.
Thanks for the solution for the product. However, how do I get the green plot (i.e. the blue plot shifted in the negative x direction) to show up correctly? As I want to then multiply the green plot with the red plot. Thanks again.
My pleasure.
I may be missing something, but it looked to me that the shifted plot and all the others do plot correctly as in your posted code, except for the order-of-magnitude higher frequency vectors, at least when I plotted it.
All I did was multiply the data you provided, and then plotted it as the product, as a function of the appropriate frequency vector.
OK, I see where the problem would be...If I multiply the frequency of plot 2 to get the green plot, the x data from plot 1 will not match the x data from the shifted plot. The first 10 points of the original plots and the shifted plots will be as follows:
Note that I cannot do a direct product now between plot 1 and shifted plot 2 as the x points do not line up now. Is there a way to line up the x points so that the shifted data has the same x values like plot 1? In the data below, I looked for the first number closest to 1 in column 5 and then copied the y-axis data to row 2, column 6 and so on, then copied the x-axis data from column 2. I then had the data as follows:
As you can see, I "fudged" the data to match up.
Is there an elegant way to generate the shifted data to have matching x-axis data points like the original plots so that a direct product can then be executed? Thanks for your time and help. I hope that I made it clear :-)
My pleasure.
I am not certain how you shifted the frequencies, since you didn’t describe that. If you want to shift the data with respect to the frequencies, that would be easy: just do a circshift on the data, leaving the frequencies unshifted, then trim the last values from all records to eliminate the shifted value and last points from the other records. (An alternative would be to just trim the shifted data and then to use interp1 with the 'extrap' option to ‘create’ the missing shifted data. Your data are smooth enough to allow that.)
Or, you can shift the frequencies and keep the data unchanged. Your call.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2016년 2월 18일

댓글:

2016년 2월 19일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by