Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
improve peak detection
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsmia1939 committed Nov 30, 2022
1 parent edf6d12 commit b47f42e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
27 changes: 16 additions & 11 deletions PySimpleCV/PySimpleCV
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ cv_layout = [
[sg.Button("Open CV File"),sg.Text('.csv file with voltage on 1st colume and current on 2nd column separated by comma')],
[sg.Text('CV file:'), sg.Text('No CV file selected', key = 'CV_file_use')],
[sg.Text('jpa='), sg.Text('', key = 'output_jpa'), sg.Text('jpc='), sg.Text('', key = 'output_jpc'), sg.Text('Reversibility (jpa/jpc)='), sg.Text('', key = 'output_rev')],
[sg.Text('Trim CV plot'), sg.Slider(range=(1, 1), size=(95, 20), orientation='h', key='sl_cut_val', enable_events=True, disabled=True)],
[sg.Text('Start 1'),sg.Slider(range=(1, 1), size=(95, 20), orientation='h', key='sl_jpa_lns', enable_events=True, disabled=True)],
[sg.Text('End 1'),sg.Slider(range=(1, 1), size=(95, 20), orientation='h', key='sl_jpa_lne', enable_events=True, disabled=True)],
[sg.Text('Start 2'),sg.Slider(range=(1, 1), size=(95, 20), orientation='h', key='sl_jpc_lns', enable_events=True, disabled=True)],
[sg.Text('End 2'),sg.Slider(range=(1, 1), size=(95, 20), orientation='h', key='sl_jpc_lne', enable_events=True, disabled=True)],
[sg.Text('Trim CV plot'), sg.Slider(range=(1, 1), size=(95, 10), orientation='h', key='sl_cut_val', enable_events=True, disabled=True)],
[sg.Text('Start 1'),sg.Slider(range=(1, 1), size=(95, 10), orientation='h', key='sl_jpa_lns', enable_events=True, disabled=True)],
[sg.Text('End 1'),sg.Slider(range=(1, 1), size=(95, 10), orientation='h', key='sl_jpa_lne', enable_events=True, disabled=True)],
[sg.Text('Start 2'),sg.Slider(range=(1, 1), size=(95, 10), orientation='h', key='sl_jpc_lns', enable_events=True, disabled=True)],
[sg.Text('End 2'),sg.Slider(range=(1, 1), size=(95, 10), orientation='h', key='sl_jpc_lne', enable_events=True, disabled=True)],

[sg.Slider(range=(1, 100), size=(95, 20), orientation='h', key='peak_range', enable_events=True, disabled=False)],
[sg.Slider(range=(1, 1), size=(95, 20), orientation='h', key='peak_pos', enable_events=True, disabled=False)],
[sg.Slider(range=(1, 1), size=(95, 20), orientation='h', key='trough_pos', enable_events=True, disabled=False)],
[sg.Text('Peak search range'),sg.Slider(range=(1, 100), size=(95, 10), orientation='h', key='peak_range', enable_events=True, disabled=False)],
[sg.Text('Top peak position'),sg.Slider(range=(1, 1), size=(95, 10), orientation='h', key='peak_pos', enable_events=True, disabled=False)],
[sg.Text('Bottom peak position'),sg.Slider(range=(1, 1), size=(95, 10), orientation='h', key='trough_pos', enable_events=True, disabled=False)],

[sg.Button('Clear plot', key='cv_clear'),sg.Button('Exit', key='exit'),sg.Button('Save plot', key='save_cv'),sg.Button('About PySimpleCV'),sg.Spin([i for i in range(1,11)], initial_value=10, k='-SPIN-')]
]
Expand Down Expand Up @@ -153,7 +153,7 @@ while True:
window['sl_jpc_lne'].Update(disabled=False)
window['CV_file_use'].Update(CV_file)
ax_cv.grid()
ax_cv.plot(volt, current, '-')
ax_cv.plot(volt, current, '-', color='black')
ax_cv.grid()
fig_agg_cv.draw()
except Exception as file_error:
Expand Down Expand Up @@ -183,8 +183,8 @@ while True:


# Start plotting
jpa_lns,jpa_lne,jpc_lns,jpc_lne, volt, current, jpa_base, jpa, jpc_base, jpc = get_CV(df_CV,jpa_lns,jpa_lne,jpc_lns,jpc_lne,peak_volt,trough_volt)
ax_cv.plot(volt[cut_val:], current[cut_val:])
jpa_lns,jpa_lne,jpc_lns,jpc_lne, volt, current, jpa_base, jpc_base = get_CV(df_CV,jpa_lns,jpa_lne,jpc_lns,jpc_lne,peak_volt,trough_volt)
ax_cv.plot(volt[cut_val:], current[cut_val:], '-', color='black')

ax_cv.plot(peak_volt,jpa_base,marker='s')
ax_cv.plot(volt[jpa_lns:jpa_lne],current[jpa_lns:jpa_lne],linewidth=4,linestyle='-',color='red')
Expand All @@ -198,6 +198,11 @@ while True:
ax_cv.plot((volt[jpc_lns],trough_volt),(current[jpc_lns],jpc_base),'--')

fig_agg_cv.draw()
jpa = peak_curr - jpa_base
jpc = jpc_base - trough_curr
window['output_jpa'].Update(np.round(jpa,3))
window['output_jpc'].Update(np.round(jpc,3))
window['output_rev'].Update(np.round((jpa/jpc),3))
case "save_cv":
save_cv_loc = sg.popup_get_file('Create of choose a file (PNG) to save to', save_as=True)
if save_cv_loc is None:
Expand Down
19 changes: 11 additions & 8 deletions PySimpleCV/PySimpleCV_main_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,17 @@ def min_value(set_value,input_val):

def get_CV_peak(df_CV, peak_range, peak_pos, trough_pos):
cv_size, volt, current = get_CV_init(df_CV)
high_range_peak = max_value(cv_size-1,peak_pos+peak_range)
low_range_peak = min_value(0,peak_pos-peak_range)

high_range_peak = np.where((peak_pos+peak_range)>=(cv_size-1),(cv_size-1),peak_pos+peak_range)
low_range_peak = np.where((peak_pos-peak_range)>=0,peak_pos-peak_range,0)
peak_curr_range = current[low_range_peak:high_range_peak]
peak_curr = max(peak_curr_range)
peak_idx = np.argmin(np.abs(peak_curr_range-peak_curr))
peak_volt = volt[low_range_peak:high_range_peak][peak_idx]

high_range_trough = max_value(cv_size-1,trough_pos+peak_range)
low_range_trough = min_value(0,trough_pos-peak_range)

high_range_trough = np.where((trough_pos+peak_range)>=(cv_size-1),(cv_size-1),trough_pos+peak_range)
low_range_trough = np.where((trough_pos-peak_range)>=0,trough_pos-peak_range,0)

trough_curr_range = current[low_range_trough:high_range_trough]
trough_curr = min(trough_curr_range)
trough_idx = np.argmin(np.abs(trough_curr_range-trough_curr))
Expand All @@ -143,15 +145,16 @@ def get_CV(df_CV,jpa_lns,jpa_lne,jpc_lns,jpc_lne,peak_volt,trough_volt):

cv_size, volt, current = get_CV_init(df_CV)


jpa_lnfit = np.polyfit(volt[jpa_lns:jpa_lne],current[jpa_lns:jpa_lne], 1)
jpa_base = jpa_lnfit[0]*peak_volt + jpa_lnfit[1]
jpa = peak_volt - jpa_base
# jpa = peak_volt - jpa_base

jpc_lnfit = np.polyfit(volt[jpc_lns:jpc_lne],current[jpc_lns:jpc_lne], 1)
jpc_base = jpc_lnfit[0]*trough_volt + jpc_lnfit[1]
jpc = trough_volt - jpc_base
# jpc = trough_volt - jpc_base

return jpa_lns,jpa_lne,jpc_lns,jpc_lne, volt, current, jpa_base, jpa, jpc_base, jpc
return jpa_lns,jpa_lne,jpc_lns,jpc_lne, volt, current, jpa_base, jpc_base

def time2sec(time_raw,delim):
# Take time format such as 1-12:05:24 and convert to seconds
Expand Down
1 change: 1 addition & 0 deletions data/io.github.kevinsmia1939.PySimpleCV.metainfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<url type="bugtracker">https://github.com/kevinsmia1939/PySimpleCV/issues</url>
<content_rating type="oars-1.1"/>
<releases>
<release version="v0.6.0" date="2022-11-30"/>
<release version="v0.5.4" date="2022-11-22"/>
<release version="v0.5.3" date="2022-11-21"/>
<release version="v0.5.2" date="2022-11-21"/>
Expand Down

0 comments on commit b47f42e

Please sign in to comment.