Skip to content

Commit

Permalink
Update the code to 0.6 (#2)
Browse files Browse the repository at this point in the history
* Upgrade the module to 0.6

* update to 0.6

* update to 0.6

* Up the version requirement.

* Update .travis.yml

* Update .travis.yml

* Fix struct rename
  • Loading branch information
dhoegh authored Nov 14, 2017
1 parent d9ee926 commit 264f339
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 52 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
language: julia
os:
- linux
- osx
julia:
- 0.3
- 0.4
- 0.6
- nightly
notifications:
email: false
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ using Rainflow

signal = randn(100) # Gennerates some random data
ext, t = sort_peaks(signal) # Sorts the signal to local extrema's, could optional take a time vector
plot([0:length(signal)-1], signal)
plot(collect(0:length(signal)-1), signal)
plot(t,ext,"ro") # plots extrema's
cycles = count_cycles(ext,t) # find all the cycles in the data
plot(cycles) # plot of a whole cycle is not plottet correctly, it plots a cylce from the starting point to where the value that defines the range occur.
cycles = count_cycles(ext, t) # find all the cycles in the data
plot.(cycles) # plot of a whole cycle is not plottet correctly, it plots a cylce from the starting point to where the value that defines the range occur.
figure()
plot(cycles[1])
bins = sum_cycles(cycles,10,1) # Sums the cycles together dependant on intervals, here there is 10 range interval and one mean interval
bins = sum_cycles(cycles, 10, 1) # Sums the cycles together dependant on intervals, here there is 10 range interval and one mean interval
figure()
bar([1:length(bins)],squeeze(bins,2),0.75)
bar(collect(1:length(bins)), squeeze(bins,2), 0.75)

range_intervals = [0, 40, 45, 50, 55, 60, 100.] # There can also be specified user defined intervals
mean_intervals = [0.,100.] # The intevals needs to go from 0-100
bins = sum_cycles(cycles, range_intervals, mean_intervals) # Sums the cycles in the given intervals
figure()
bar([1:length(bins)],squeeze(bins,2),0.75)
bar(collect(1:length(bins)), squeeze(bins,2), 0.75)
```

## Performance note
Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
julia 0.3
julia 0.6
84 changes: 47 additions & 37 deletions src/Rainflow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import Base.show
export sort_peaks, find_boundary_vals, count_cycles, sum_cycles

""" This function sort out points where the slope is changing sign."""
function sort_peaks(signal::AbstractArray{Float64,1}, dt=[0.:length(signal)-1.])
function sort_peaks(signal::AbstractArray{Float64,1}, dt=collect(0.:length(signal)-1.))
slope = diff(signal)
# Determines if the point is local extremum
is_extremum = [true, (slope[1:end-1].*slope[2:end]).<=0., true]
is_extremum = vcat(true, (slope[1:end-1].*slope[2:end]).<=0., true)
return signal[is_extremum] , dt[is_extremum]
end

immutable Cycle # This is the information stored for each cycle found
struct Cycle # This is the information stored for each cycle found
count::Float64
range::Float64
mean::Float64 # value
Expand All @@ -34,7 +34,7 @@ function count_cycles(ext_in::Array{Float64,1},t::Array{Float64,1})
i = 1
j = 2
cycles = Cycle[]
sizehint(cycles, length(ext)) # This reduces the memory consumption a bit
sizehint!(cycles, length(ext)) # This reduces the memory consumption a bit
@inbounds begin
while length(ext)>(i+1)
Y = abs(ext[i+1]-ext[i])
Expand All @@ -47,11 +47,10 @@ function count_cycles(ext_in::Array{Float64,1},t::Array{Float64,1})
shift!(time)
else # This case counts one cycle and deletes the poit that is counted
#println("One cycle $(ext[i]), $(ext[i+1])")
push!(cycles,cycle(1. ,ext[i], time[i], ext[i+1],time[i+1]))
splice!(ext,i+1) # Removes the i and i+1 entrance in ext and time
splice!(ext,i)
splice!(time,i+1)
splice!(time,i)
push!(cycles,cycle(1. ,ext[i], time[i], ext[i+1], time[i+1]))
# Removes the i and i+1 entrance in ext and time
my_deleteat!(ext, i:(i+1))
my_deleteat!(time, i:(i+1))
end
i = 1
j = 2
Expand All @@ -68,7 +67,7 @@ function count_cycles(ext_in::Array{Float64,1},t::Array{Float64,1})
return cycles
end

type Cycles_bounds #
mutable struct Cycles_bounds #
min_mean::Float64
max_mean::Float64
max_range::Float64
Expand All @@ -89,48 +88,40 @@ end

""" Returns the range index the value is belonging in """
function find_range{T<:Real}(interval::Array{T,1},value)
issorted(interval) || error("The array needs to be sorted in raising order")
for i=1:length(interval)-1
if interval[i] <= value <= interval[i+1]
if interval[i] <= value < interval[i+1]
return i
end
end
error("The value where not in range")
end

if v"0.4.0-dev+4986" < VERSION
typealias Interval{T} Union{Array{T,1}, LinSpace{T}}
""" Returns the range index the value is belonging in """
function find_range{T<:Real}(interval::LinSpace{T}, value)
issorted(interval) || error("The array needs to be sorted in raising order")
start = interval.start
stop = interval.stop
(start < value < stop) || error("The value where not in range, see if the vectors in calc_sum(cycles::Array{Cycle,1}, range_intervals::Array{T,1}, mean_intervals::Array{T,1}) are continious increasing in value, or adjust the nr_digits parameter")
inc = (interval.stop - start) / interval.divisor
i = int(fld(value - start, inc) ) + 1
end
else
typealias Interval{T} Array{T,1}
end

Interval{T} = Union{Array{T,1}, StepRangeLen{T}}

""" Sums the cycle count given intervals of range_intervals and mean_intervals. The range_intervals and mean_intervals is given in fraction of range size"""
function sum_cycles{T<:Real}(cycles::Array{Cycle,1}, range_intervals::Interval{T}, mean_intervals::Interval{T})
bounds = find_boundary_vals(cycles)
bins = zeros(length(range_intervals)-1, length(mean_intervals)-1)
range_intervals *= bounds.max_range/100
#show(range_intervals)
mean_intervals *= (bounds.max_mean-bounds.min_mean)/100
mean_intervals += bounds.min_mean
range_in = (range_intervals*bounds.max_range)/100
mean_in = (mean_intervals*(bounds.max_mean-bounds.min_mean))/100
mean_in += bounds.min_mean
issorted(mean_intervals) || error("The array needs to be sorted in raising order")
issorted(range_intervals) || error("The array needs to be sorted in raising order")
nr_digits = 14 # The rounding is performed due to numerical noise in the floats when comparing
if v"0.4.0-dev+4986" > VERSION || isa(mean_intervals,LinSpace)
mean_intervals = round(mean_intervals, nr_digits)
elseif v"0.4.0-dev+4986" > VERSION || isa(range_intervals,LinSpace)
range_intervals = round(range_intervals, nr_digits)
end
mean_i = collect(mean_in)
range_i = collect(range_in)
#ensure the cycles is in the intervals by adding a small error to the end values of the interal.
error_m = (bounds.max_mean-bounds.min_mean)*1e-14
mean_i[end]+=error_m
mean_i[1]-=error_m
error_r = bounds.max_range*1e-14
range_i[end]+=error_r
range_i[1]-=error_r
#show(mean_intervals)
for cycle in cycles
i = find_range(range_intervals,round(cycle.range, nr_digits))
j = find_range(mean_intervals,round(cycle.mean, nr_digits))
i = find_range(range_i, cycle.range)
j = find_range(mean_i, cycle.mean)
bins[i,j] += cycle.count
end
return bins
Expand All @@ -142,6 +133,25 @@ function sum_cycles(cycles::Array{Cycle,1}, nr_ranges::Int=10, nr_means::Int=1)
sum_cycles(cycles, range_intervals, mean_intervals)
end

# This is necersary because of https://github.com/JuliaLang/julia/issues/24494
if VERSION<v"0.6.2" && Base.is_windows()
function my_deleteat!(a::Vector, r::UnitRange{<:Integer})
n = length(a)
isempty(r) || _deleteat_beg!(a, first(r), length(r))
return a
end
function _deleteat_beg!(a::Vector, i::Integer, delta::Integer)
if i > 1
ccall(:memmove, Ptr{Void}, (Ptr{Void}, Ptr{Void}, Csize_t),
pointer(a, 1+delta), pointer(a, 1), (i-1)*Base.elsize(a))
end
ccall(:jl_array_del_beg, Void, (Any, UInt), a, delta)
return a
end
else
const my_deleteat! = deleteat!
end

try
include("plot.jl")
catch
Expand Down
8 changes: 3 additions & 5 deletions src/plot.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import PyPlot.plot

function plot(cycle::Cycle)
time = linrange(cycle.t_s,cycle.t_e,25)
time = linspace(cycle.t_s, cycle.t_e, 25)
amplitude = abs(cycle.v_s-cycle.v_e)/2
dt = cycle.t_s-cycle.t_e
if cycle.count==1
plot(time, cycle.mean+sign(cycle.v_s-cycle.v_e)*amplitude*cos(2π/dt*(time-cycle.t_s)))
plot(time, cycle.mean.+sign(cycle.v_s-cycle.v_e).*amplitude.*cos.(2π/dt.*(time-cycle.t_s)))
else
plot(time, cycle.mean+sign(cycle.v_s-cycle.v_e)*amplitude*cos/dt*(time-cycle.t_s)))
plot(time, cycle.mean.+sign(cycle.v_s-cycle.v_e).*amplitude.*cos./dt.*(time-cycle.t_s)))
end
end

plot(cycles::Array{Cycle,1}) = for cyc in cycles plot(cyc) end

0 comments on commit 264f339

Please sign in to comment.