網頁

2011年6月1日 星期三

MATLAB save neural network object to file

it is a easy operation that saves the neural network object to MAT-file in the MATLAB, but the problem is how to load the neural network object from MAT-file.

save the neural network object
    save('./dat/bpnn_00.mat', 'bpnn'); 
    %    | file name      |  nn obj | // it`s easy.

load the neural network object
    newData1 = load('-mat', './dat/bpnn_00.mat');
    
    % transfer variable format
    vars = fieldnames(newData1);
    for i = 1:length(vars)
        assignin('base', vars{i}, newData1.(vars{i}));
    end

MATLAB get 'bpnn' of the neural network object, but the neural network can`t simulate. I don`t know why the neural network object can`t directly simulate.
    nnResult = sim(bpnn, nnInput); % nnResult is error value

I add a 'getx' function to program, and 'nnResult' of the neural network is correct value. Why?
    nnWB = getx(bpnn); % <-- why? 
    nnResult = sim(bpnn, nnInput); % nnResult is correct value

It maybe the weight and bias value don`t put to the variable of the neural network tool when MATLAB load the MAT-file.

2011年5月16日 星期一

STM32F103 Timer - base operation

Using Timer2 of STM32F103 to trigger Interrupt every 1us, and the initiation is shown as follow.
void init_timer(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
 
    TIM_TimeBaseStructure.TIM_Period = 0xA;     // Counter 12 = 1Mhz = 1us        
    TIM_TimeBaseStructure.TIM_Prescaler = 0x5;  // 72Mhz/6 = 12Mhz       
    TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;    
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
 
    TIM_ClearFlag(TIM2, TIM_FLAG_Update);
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM2, ENABLE);
}

The program counter goes into TIM2_IRQHandler function when the timer2 trigger interrupt flag.

void TIM2_IRQHandler(void)
{
    // Timer2 Interrupt Handler
}