Getting decrease in a sine wave as a function of time
Show older comments
I am trying to fit an exponential function to the peaks in a decreasing sinusoid:

I tried importing the values for time and position like this because the decimal places are marked with commas in my data that should be dots in MatLab:
% Open data file
fid = fopen('Data.csv');
% Read data file
readData = textscan(fid,'%f %f','HeaderLines',1,'Delimiter',',');
% Extract data from readData
time = readData{1,1}(:,1);
position = readData{1,2}(:,1);
% Plot data
plot (time,position,'bo-')
Is this correct? The plot looks odd. Attached Data.csv if one wanted to check. The file consists of two long columns of values.
Next I'm thinking of using the peakfinder (I am not sure about its input and output at all):
C = vertcat(time, position);
peakfinder(C)
And finally fit the values using polyfit? It should be in the form position = a * exp(-b * time) where a and b are constants. I think I should start something like this:
fit = polyfit(x, log(y), 1)
Where x and y for me are time and position from peakfinder, not sure how to write those at all.
3 Comments
per isakson
on 24 Dec 2016
Edited: per isakson
on 24 Dec 2016
- Concerning the decimal separator see http://uk.mathworks.com/matlabcentral/answers/57276-import-data-file-with-comma-decimal-point#answer_69283. Your code doesn't read the file correctly with R2016a.
- The Import Data tool interprets comma as decimal separator correctly.

 
- "to the peaks in a decreasing sinusoid"   see env_secant(x_data, y_data, view, side) by Andreas Martin
Star Strider
on 24 Dec 2016
Thank you, Per. I didn’t realise there is a comma decimal separator. I solved that problem with textscan, then strrep, and str2double.
Daniel Holmberg
on 24 Dec 2016
Accepted Answer
More Answers (1)
Roger Stafford
on 24 Dec 2016
The data you show should probably be fitted with something like:
a*exp(-b*time)*sin(c*time+d)
with the four parameters, a, b, c, and d, to be adjusted. It’s known as an exponentially decaying sinusoidal signal.
1 Comment
Daniel Holmberg
on 24 Dec 2016
Categories
Find more on Digital Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!